Subscribe on changes!

Incorrect type inferring with generic components and withDefaults

avatar
May 15th 2023

Vue version

3.3.2

Link to minimal reproduction

https://stackblitz.com/edit/vitejs-vite-nwkbil?file=src/components/ListGeneric.vue

Steps to reproduce

Steps to reproduce

  1. Open a new Terminal after successful setup of dependencies
  2. type and run npm run typecheck into the terminal
  3. See an error in ListGeneric.vue file

What is expected?

There is no error occurred

What is actually happening?

TS error

TS2345: Argument of type '{ sameModel: string; }' is not assignable to parameter of type 'InferDefaults<DefineProps<{ modelValue: T; sameModel?: T | undefined; }>>'.

System Info

No response

Any additional comments?

No response

avatar
May 16th 2023

This is work as intended

For example:

function someFunction<T extends string>(
    sameModel: T = 'sameModel default'
) {
}
Type 'string' is not assignable to type 'T'.
  'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'.ts(2322)

So withDefaults should throw a error for this, also.

avatar
May 16th 2023

if i change define props to

withDefaults(
  defineProps<{
    modelValue: T;
    sameModel?: string;
  }>(),
  {
    sameModel: 'sameModel default',
  }
);

its stil throws error

TS2345: Argument of type '{ sameModel: string; }' is not assignable to parameter of type 'InferDefaults<DefineProps<{ modelValue: T; sameModel?: string | undefined; }>>'.
avatar
May 16th 2023

@grindpride This is another error. duplicated to https://github.com/vuejs/core/issues/8331