Closed
Description
Bug Report
π Search Terms
contextual type inference function return generic
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics.
β― Playground Link
Playground link with relevant code
π» Code
interface Options<K> {
key: K;
fn(key: K): any;
}
function id<K>(options: () => Options<K>) {
throw 'unimplemented';
}
const breaks = id(() => {
return {
key: { id: 32 },
fn(key) {
// 'key' is of type 'unknown'.(18046)
return key.id.toString();
},
};
});
const works = id(() => ({
key: { id: 32 },
fn(key) {
return key.id.toString();
},
}));
π Actual behavior
The fn
parameter type is only correctly inferred in the case where an arrow function is directly returning an object.
π Expected behavior
I would expect for both cases to be handled consistently.
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
RyanCavanaugh commentedon Apr 21, 2023
Duplicate #47599
jcalz commentedon Apr 21, 2023
@RyanCavanaugh Is a block body context sensitive in a way that a concise body isnβt? I suspect as much but Iβd love to hear official word since #47599 doesnβt seem to talk about that specifically.
ahejlsberg commentedon Apr 22, 2023
This isn't related to #47599. Rather, it's an issue that was already fixed in #50903. The example works as expected in 5.1.
ahejlsberg commentedon Apr 22, 2023
So, I'll call this a duplicate of #50687.
typescript-bot commentedon Apr 24, 2023
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.