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
{typeFn1=(x: number)=>number;typeFn2=(x: any)=>string;constfn=1asanyasFn1|Fn2fn(1)// error: Cannot invoke an expression whose type lacks a call signature.}
However, if I change Fn2 to type Fn2 = (x: number) => string; (thus removing the any), the error disappears.
I would like to understand why TypeScript can't handle the former case.
For context, I am trying to build an Option type, where Option = Some | None:
{classSome<T>{constructor(publict: T){}map<T2>(f: (t: T)=>T2): Some<T2>{returnnewSome(f(this.t))}}classNone{constructor(){}map(f: (t: any)=>any): None{returnnewNone()}get(){thrownewError('foo')};}typeOption<T>=Some<T>|None;constoption=newSome(1)asOption<number>;option.map(x=>1)// error: Cannot invoke an expression whose type lacks a call signature.}
The text was updated successfully, but these errors were encountered:
A union type has only gets signatures that are identical in all constituents. the reason is there is no meaningful way the compiler can combine these signatures, for the general case, and still grantee type safety. see #10620 for more details.
v2.1.4
However, if I change
Fn2
totype Fn2 = (x: number) => string;
(thus removing theany
), the error disappears.I would like to understand why TypeScript can't handle the former case.
For context, I am trying to build an
Option
type, whereOption = Some | None
:The text was updated successfully, but these errors were encountered: