Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
hrubysi opened this issue Feb 20, 2024 · 2 comments

Comments

@pikax
Copy link
Member

pikax commented Feb 28, 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

@pikax pikax closed this as completed Feb 28, 2024
@hrubysi
Copy link
Author

hrubysi commented Feb 28, 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

@github-actions github-actions bot locked and limited conversation to collaborators Mar 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants