Subscribe on changes!

Component generic infered from return type of a function prop with argument is unknown

avatar
Feb 28th 2024

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 image

avatar
Feb 28th 2024

@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.

image image