vuejs / test-utils

Vue Test Utils for Vue 3
https://test-utils.vuejs.org
MIT License
1.03k stars 245 forks source link

Bug: 3.5 alpha type issue #2483

Closed yyx990803 closed 2 months ago

yyx990803 commented 2 months ago

Discovered when running latest ecosystem-ci on minor branch in Vue core: https://github.com/vuejs/ecosystem-ci/actions/runs/10194241059/job/28200426892

The relevant line here: https://github.com/vuejs/test-utils/blob/main/src/createInstance.ts#L263

The type error appeared after we added app.config.idPrefix in 3.5.0-alpha.3, due to an interesting constraint in TypeScript:

declare const x: {
  a: boolean // required primitive type
  b?: string // optional, different primitive type
}

declare var k: keyof typeof x

x[k] = true as any
// ^ type error!

However, this works:

declare const x: {
  a: {} // required non-primitive type
  b?: string // optional, different primitive type
}

declare var k: keyof typeof x

x[k] = true as any // somehow works

The code in test-utils was previously working because it was case 2. After app.config.idPrefix was added it became case 1.

I don't think Vue core needs to change anything as it's mostly a usage issue in test-utils. Not sure if there's even a good workaround for this. Maybe this case should just be // @ts-expect-error...