-1
exports interface dev {
    print(
        a: string,
        b: string
    ):Promise<class bb>;
}



export function printInstance: dev {

   return new(class implements dev {

    public async print(
        C: string
    ): Promise<class bb> {

    }
})()
}

In interface there is two input a and b but in implementation another input c is introduced which doesn't match with name in interface and only one is send. Will it cause any issues? I tried this and no error was thrown I guess name of variable doesn't matter and if we pass less parameters then then the other one will take null value.

Chaosfire
  • 4,818
  • 4
  • 8
  • 23
Akash
  • 1
  • 1
  • @Chaosfire it's not JavaScript either. It's not even TypeScript afaict. – Bergi Aug 29 '23 at 18:06
  • 2
    What language is this? You originally tagged it as Java, but it doesn't look like it. It's not JavaScript or TypeScript either, there are several syntax errors. – Bergi Aug 29 '23 at 18:07
  • Assuming this is a language that compiles to JavaScript though, [do not use `new (class { … })()`](https://stackoverflow.com/q/38739499/1048572)! – Bergi Aug 29 '23 at 18:09

2 Answers2

-1

Typescript uses structural typing to determine type compatibility. For functions this means that you don't need to have the exact same signature for the declaration and the implementation, as long as the compiler can determine the implementation to be safe to call through the declaration.

In this case this boils down to, a function with less parameters can be an implementation for a function declaration with more parameters as the extra passed in parameters will be ignored by the implementation, so no runtime error can occur because of this (for the most cases anyway, there might be corner cases on things that depend on Function.length)

It means Typescript doesn't check arg types or even count)

-1

Your provided code seems to have syntax issues,

export interface Dev {
    print(a: string, b: string): Promise<Bb>;
}

class Bb {}

export const printInstance: Dev = {
    async print(c: string, d?: string): Promise<Bb> {
      
        return new Bb();
    }
}

I made the second parameter (d) optional by using the ? syntax. This means you can call the print method with one or two parameters. If you only pass one parameter, the second one (d) will be undefined.

AKASH
  • 95
  • 3