#My type checker doesn't inherit types from interface, but checks for them

27 messages · Page 1 of 1 (latest)

cobalt hawk

Hi my interface looks like this: ```ts
interface OptionManagerInterface {
get(name: string, required: false): string | null;
get(name: string, required: true): string;
get(name: string, required: boolean): string | null;
}

but when i implement the interface the types arent inherited (img)

but they are checked for (img)
sterile monolithBOT
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
cobalt hawk

functional code: ```ts
export class OptionManager implements OptionManagerInterface {
private builder: PrefixBuilder;
args: string[];

constructor(args: string[], builder: PrefixBuilder) {
    this.builder = builder;
    this.args = args;
}

get(name, required) {    
    
    var indice: number | null = null;

    for (let index = 0; index < this.builder.args.length; index++) {
        const arg = this.builder.args[index]
        if (arg.name == name) {
            indice = index;
        }
    }
    
    if (!indice) {
        throw new Error(`Name ${name} not recognized.`)
    }
    
    if (indice || required) {
        return this.args[indice];
    }

    return null;
}

}

median agate

Because that's how it behaves, you need to copy the overloads above the function as well, because it checks implementation signatures against all overloads.

Also there is a findIndex option to search for indexes

cobalt hawk

i remember this one repo did this

median agate

I mean when using the function in actual codebase

const xyz: someinterface = new Xyz()
xyz.get()``` you will get the intellisense, it's just so after you could those inside the class itself and without specifically attaching a type to it later
cobalt hawk
median agate
class XyzInterface {
  xyz(name: string, required: false): string | null;
  xyz(name: string, required: true): string;
}

class Xyz implements XyzInterface {
  xyz(name: string, required: false): string | null;
  xyz(name: string, required: true): string;
  xyz(name: string, required: boolean): string | null {
    
  }
}
``` If you implement overloads in the class, you dont need to attach types later to it, otherwise when initializing the class
```ts
const xyz: XyzInterface = new Xyz()
xyz.xyz()```  you can attach the type and intellisense will work
cobalt hawk

what?

the interface is literally useless

also implementation files .ts dont allow overloads im pretty sure

what the fuck is this language

i hate this

jdphpfoiuhdsopfhsdopfhpsdhfidfosdhidhfosifheofohfhoehof

what the fuck this exists now??

also why are theres 25 ^ 10 files for each fucking type

i hate this so much

fuck this

cobalt hawk

*they do

median agate

Thank you for the correction, I hope and believe it will fix your issues

alpine hamlet
alpine hamlet

Link to the repo then and we can help you how to achieve what they did

prime shell

and you don't want to search in the entire array after you've already found it