I use a 3rd party library that defines an interface (A) with an overloaded method (method). The method can accept either null or string value as a parameter.
I define a union type: string | null and pass it as a parameter into the method, but I get an error:
No overload matches this call.
Overload 1 of 2, '(val: null): null', gave the following error.
Argument of type 'StringOrNull' is not assignable to parameter of type 'null'.
Type 'string' is not assignable to type 'null'.
Overload 2 of 2, '(val: string): string', gave the following error.
Argument of type 'StringOrNull' is not assignable to parameter of type 'string'.
Type 'null' is not assignable to type 'string'.(2769)
Here is my code: TypeScript Playgroud
interface A{
method(val: null): null;
method(val: string): string;
}
type StringOrNull = string | null;
function myFunc(obj: A, val: StringOrNull) {
obj.method(val); // Error: No overload matches this call.
}
Why does this happen and how I can fix this?