Closed senyaak closed 7 years ago
I have tried some more cases and got a questin:
var b: B;
interface A {
foo(name: string, callback: () => void);
}
interface B extends A {
foo(name: 'John', callback: (age: number) => void); //foo type 1
foo(name: string, callback: () => void); //foo type 2
}
b.foo("John",(a) => {});
in this case b.foo has type "foo type 2" I think the compiler should throw an error that the function ()=>void is not assignable to type (age: number) => void, shouldn't it?
@senyaak I can't repro your error on the playground
Is there some combination of tsconfig options that I am missing?
Regarding your second question, a function with zero parameters is assignable to a function with one parameter because the zero-param function with just ignore the one argument:
type Nullary = () => void;
type Unary = (a: string) => void;
let n: Nullary;
let u: Unary;
/// not ok ///
n = u // not ok
n() // u expected an argument!
/// ok ///
u = n
u(12) // ok, n will ignore its argument
This is important to make map
usable, since it passes 3 arguments to its callback, but the callback is almost always written with only 1 parameter.
@sandersn Thank you for your answer. Have only atom settings: "compileOnSave": false, "buildOnSave": false, "noEmitOnError": false,
in my tsconfig
This is important to make map usable, since it passes 3 arguments to its callback, but the callback is almost always written with only 1 parameter.
I think it's a broken behavior, the code isn't type save now.. The optional parameters should be used in this case Update: Besides I think the string literal types should have higher priority than string types, otherwise it makes no sense if you have case like this...
Tried on the other pc - it works now Oo Can be closed.
Thanks for checking.
Here's the FAQ entry on why functions with fewer parmaeters are assignable to functions with more: https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-functions-with-fewer-parameters-assignable-to-functions-that-take-more-parameters
TypeScript Version: 2.2.1
Code
Expected behavior: " this does not work" - part should compile without errors Actual behavior: The compiler throw an error: