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
Domain: Error Messages arrow function annotation fresh return type
Suggestion
Currently, when an arrow function is annotated with a function signature type, any errors which occur point to the variable being assigned. Here's an example:
typeSplitName=(name: string)=>{first: string;last: string;}constsplitName: SplitName=(name)=>{// ^ Type '(name: string) => { first: string; }' is not assignable to type 'SplitName'.// Property 'last' is missing in type '{ first: string; }' but required in type '{ first: string; last: string; }'.(2322)// input.tsx(3, 5): 'last' is declared here.const[first,lost]=name.split(" ");return{ first, lost };}
Because this function's arguments match the signature, it should be possible to give a more specific error:
constsplitName: SplitName=(name)=>{const[first,lost]=name.split(" ");return{ first, lost };// ^ Type '{ first: string; lost: string; }' is not assignable to type '{ first: string; last: string; }'.// Object literal may only specify known properties, and 'lost' does not exist in type '{ first: string; last: string; }'.}
This is very helpful in more complex cases.
This behavior would match the error message shown when manually annotating the return value of the function. Using ReturnType<> matches the proposed error message exactly, for all return values I've tried, provided that the type is not a union:
constsplitName: SplitName=(name): ReturnType<SplitName>=>{const[first,lost]=name.split(" ");return{ first, lost };// ^ Type '{ first: string; lost: string; }' is not assignable to type '{ first: string; last: string; }'.// Object literal may only specify known properties, and 'lost' does not exist in type '{ first: string; last: string; }'.}
It might be best to avoid applying this elaboration to union types of any kind, as the error messages end up being applied to both the variable and the return value, which ends up being worse than the current error:
typeSplitNameObj=(name: string)=>{first: string,last: string};typeSplitNameTuple=(name: string)=>[first: string,last: string];typeSplitName=SplitNameObj|SplitNameTuple;constsplitName: SplitName=(name): ReturnType<SplitName>=>{// ^ Type '(name: string) => { first: string; last: string; } | [first: string, last: string]' is not assignable to type 'SplitName'.// Type '(name: string) => { first: string; last: string; } | [first: string, last: string]' is not assignable to type 'SplitNameObj'.// Type '{ first: string; last: string; } | [first: string, last: string]' is not assignable to type '{ first: string; last: string; }'.// Type '[first: string, last: string]' is not assignable to type '{ first: string; last: string; }'.(2322)const[first,lost]=name.split(" ");constreturnValue={ first, lost };returnreturnValue;// ^ Type '{ first: string; lost: string; }' is not assignable to type '{ first: string; last: string; } | [first: string, last: string]'.// Property 'last' is missing in type '{ first: string; lost: string; }' but required in type '{ first: string; last: string; }'.(2322)// input.tsx(1, 55): 'last' is declared here.}
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript/JavaScript code
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
Search Terms
Domain: Error Messages arrow function annotation fresh return type
Suggestion
Currently, when an arrow function is annotated with a function signature type, any errors which occur point to the variable being assigned. Here's an example:
Because this function's arguments match the signature, it should be possible to give a more specific error:
This is very helpful in more complex cases.
This behavior would match the error message shown when manually annotating the return value of the function. Using
ReturnType<>
matches the proposed error message exactly, for all return values I've tried, provided that the type is not a union:It might be best to avoid applying this elaboration to union types of any kind, as the error messages end up being applied to both the variable and the return value, which ends up being worse than the current error:
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: