You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var foo: (number) => void = (bar: string) => bar.substr(0);
According to spec, 3.10.4 Assignment Compatibility:
M is a non-specialized call or construct signature and S has an apparent call or construct signature N where, when M and N are instantiated using type Any as the type argument for all type parameters declared by M and N (if any),
the signatures are of the same kind (call or construct)
M has a rest parameter or the number of non-optional parameters in N is less than or equal to the total number of parameters in M
for parameter positions that are present in both signatures, each parameter type in N is assignable to or from the corresponding parameter type in M, and
the result type of M is Void, or the result type of N is assignable to that of M.
Here, M = (number) => void, N = (string) => string. This is not a case of function argument bi-variance as in #2170, for string isn't assignable to or from number, and the assignment should fail to compile. Meanwhile, neither the playground nor TypeScript 1.5 for VS2013 complains.
Is this a bug in the compiler, or the spec is to be updated?
The text was updated successfully, but these errors were encountered:
This is an unfortunate consequence of the way function type literal syntax works in our language. In a function type literal, parameter names are mandatory and the types are optional. So in your example, you don't have a parameter of type number, you have a parameter named number of type any. Here, you can try it out on the TypeScript Playground where you'll see the correct error.
Consider this:
According to spec, 3.10.4 Assignment Compatibility:
Here, M =
(number) => void
, N =(string) => string
. This is not a case of function argument bi-variance as in #2170, forstring
isn't assignable to or fromnumber
, and the assignment should fail to compile. Meanwhile, neither the playground nor TypeScript 1.5 for VS2013 complains.Is this a bug in the compiler, or the spec is to be updated?
The text was updated successfully, but these errors were encountered: