Component generic infered from return type of a function prop with argument is unknown
Vue version
3.4.19
Link to minimal reproduction
Steps to reproduce
- Wrong Typescript behavior visible in
App.vue
code on line 19.
What is expected?
Return type of map
prop should be string.
What is actually happening?
Return type of map
prop is unknown when argument is present.
System Info
No response
Any additional comments?
No response
This seems to be a typescript issue rather than a vue issue, modelValue
does not have the correct type, which causes typescript to not infer it correctly, see the example playground
type NoInfer<T> = [T][T extends any ? 0 : never];
declare function func<T, Value>(opt: {
options: T[],
map: (entity: T) => Value,
modelValue: NoInfer<Value>,
}): void
type Entity = {
id: number;
name: string;
}
const entities = [
{ id: 1, name: 'a' },
{ id: 2, name: 'b' },
{ id: 3, name: 'c' },
{ id: 4, name: 'd' },
] satisfies Entity[];
func({ options: entities, map: () => 'string', modelValue: 1 })
func({ options: entities, map: (event) => 'string', modelValue: 1 })
If you change the type of modelValue
to string
it will infer correctly
@pikax
I replicated desired behavior using a function in file example.ts
in this Playgournd.
Both function and Vue component are using the same type for props and yet, their behavior is different. It seems my first assumption about map returning unknown was wrong though, that is not the cause.