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
Search Terms: Function overload, signature, declaration
Code
typeCreateElement={(tag: 'a'): HTMLAnchorElement(tag: string): HTMLElement}/* Error TS2322: Type '(tag: string) => HTMLAnchorElement | HTMLDivElement' is not assignable to type 'CreateElement'. Type 'HTMLAnchorElement | HTMLDivElement' is not assignable to type 'HTMLAnchorElement'.*/constcreateElement: CreateElement=(tag: string)=>{if(tag==='a'){returndocument.createElement('a')}returndocument.createElement('div')}
TS infers createElement's signature as (tag: string) => HTMLAnchorElement | HTMLDivElement, which is not assignable to CreateElement. Could there be a way to treat this case similarly to how TS treats function-based overloads, so instead of checking that the right-hand-side of the assignment is compatible with the left-hand type (CreateElement), TS instead checks that each case of the overload (in CreateElement) is assignable to the right-hand-side? Or if this is too radical of a departure from TS's assignability checking, could there be another way to make this work?
Expected behavior: TS should treat function and function expression overloads the same. This code should work, the same way that the following code works:
The text was updated successfully, but these errors were encountered:
bcherny
changed the title
TypeError when implementing overloaded call signature with an arrow function
Possible to implement overloaded call signature with an arrow function?
May 19, 2020
The posted "equivalent" has only one overload; it is not at all equivalent 😐
The only way to detect that this is a valid assignment is to read the function body and understand that it behaves correctly in each overload case; it is far beyond TS's capabilities to do this.
TypeScript Version: 4.0.0-dev
Search Terms: Function overload, signature, declaration
Code
TS infers
createElement
's signature as(tag: string) => HTMLAnchorElement | HTMLDivElement
, which is not assignable toCreateElement
. Could there be a way to treat this case similarly to how TS treatsfunction
-based overloads, so instead of checking that the right-hand-side of the assignment is compatible with the left-hand type (CreateElement
), TS instead checks that each case of the overload (inCreateElement
) is assignable to the right-hand-side? Or if this is too radical of a departure from TS's assignability checking, could there be another way to make this work?Expected behavior: TS should treat
function
and function expression overloads the same. This code should work, the same way that the following code works:Actual behavior: TypeError
Playground Link
Related Issues:
The text was updated successfully, but these errors were encountered: