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
{{ message }}
Repository navigation
Typing on function behaves differently if as type, interface or on the function #43365
typeCustomType={name: string}constmyFunc=(arg: string): CustomType=>{return{name: arg,extraKey: 1234};};// error: Type '{ name: string; extraKey: number; }' is not assignable to type 'CustomType'.// Object literal may only specify known properties, and 'extraKey' does not exist in type 'CustomType'.
🔎 Search Terms
Function types not erroring when using type or interface.
This is working as intended. In your no error versions you don't specify a return type for your functions, so the return type is inferred. Because the return type is inferree based on your returned value you don't get any excess properties. Then the compiler checks if your value (the function) is compatible with the annotated type of your variable (MyFunc).
I may have missed this Martin Johns (@MartinJohns). For this example, why is the return type inferred? I thought I set it explicitly as CustomType. See:
Because your anonymous arrow function has no type annotation for the return type:
// No return type annotation
(arg) => { return { name: arg, extraKey: 1234 }; }
// With return type annotation
(arg): { name: string } => { return { name: arg, extraKey: 1234 }; }
Keep in mind that additional properties are legal in TypeScript. The excess property checks feature is often called "more of a linter feature" by the TypeScript team.
Right, I understand that example Martin Johns (@MartinJohns) but since I am setting the type on the function constant, I'd expect it to adhere to the return type annotation:
Activity
MartinJohns commentedon Mar 25, 2021
This is working as intended. In your no error versions you don't specify a return type for your functions, so the return type is inferred. Because the return type is inferree based on your returned value you don't get any excess properties. Then the compiler checks if your value (the function) is compatible with the annotated type of your variable (
MyFunc).mmahalwy commentedon Mar 25, 2021
I may have missed this Martin Johns (@MartinJohns). For this example, why is the return type inferred? I thought I set it explicitly as
CustomType. See:MartinJohns commentedon Mar 25, 2021
Because your anonymous arrow function has no type annotation for the return type:
Keep in mind that additional properties are legal in TypeScript. The excess property checks feature is often called "more of a linter feature" by the TypeScript team.
mmahalwy commentedon Mar 26, 2021
Right, I understand that example Martin Johns (@MartinJohns) but since I am setting the type on the function constant, I'd expect it to adhere to the return type annotation:
RyanCavanaugh commentedon Mar 26, 2021
See also #40311 which attempts to change this
mmahalwy commentedon Mar 26, 2021
Ryan Cavanaugh (@RyanCavanaugh) thanks for linking that. Would LOVE to see that PR merged 🙏
typescript-bot commentedon Mar 28, 2021
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.