Consider this example (live example):
interface Foo<T>
{
readonly fun: (v: T) => void; // causes error below
//fun(v: T): void; // works
}
function isNumber<T>(foo: Foo<T>): foo is Foo<T & number>
{
return true;
}
this produces the error 2677:
A type predicate's type must be assignable to its parameter's type.
Type 'Foo<T & number>' is not assignable to type 'Foo<T>'.
Type 'T' is not assignable to type 'T & number'.
Type 'T' is not assignable to type 'number'.ts(2677)
But the problem goes away if we replace readonly fun: (v: T) => void;
with a member function fun(v: T): void;
. Why is this a problem, and is there a way to still use a lambda field here?
Our suspicion foo
is a (dynamic) field and it type can change later with an overwrite, therefore generic T
is not the same T
as in the Foo<T & number>
context.