Skip to content

Commit 91e54db

Browse files
committed
fix(types): Use alternative Omit
1 parent b8fc18c commit 91e54db

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

packages/dts-test/setupHelpers.test-d.ts

+25
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,31 @@ describe('defineProps w/ union type declaration + withDefaults', () => {
100100
)
101101
})
102102

103+
describe('defineProps w/ generic discriminate union + withDefaults', () => {
104+
interface B {
105+
b?: string
106+
}
107+
interface S<T> extends B {
108+
mode: 'single'
109+
v: T
110+
}
111+
interface M<T> extends B {
112+
mode: 'multiple'
113+
v: T[]
114+
}
115+
type Props = S<string> | M<string>
116+
const props = withDefaults(defineProps<Props>(), {
117+
b: 'b'
118+
})
119+
120+
if (props.mode === 'single') {
121+
expectType<string>(props.v)
122+
}
123+
if (props.mode === 'multiple') {
124+
expectType<string[]>(props.v)
125+
}
126+
})
127+
103128
describe('defineProps w/ generic type declaration + withDefaults', <T extends
104129
number, TA extends {
105130
a: string

packages/runtime-core/src/apiSetupHelpers.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,16 @@ type InferDefault<P, T> =
293293
| ((props: P) => T & {})
294294
| (T extends NativeType ? T : never)
295295

296+
// https://github.com/vuejs/core/issues/9335
297+
type OmitKeepDiscriminatedUnion<T, K extends keyof any> = T extends any
298+
? Pick<T, Exclude<keyof T, K>>
299+
: never
300+
296301
type PropsWithDefaults<
297302
T,
298303
Defaults extends InferDefaults<T>,
299304
BKeys extends keyof T
300-
> = Omit<T, keyof Defaults> & {
305+
> = OmitKeepDiscriminatedUnion<T, keyof Defaults> & {
301306
[K in keyof Defaults]-?: K extends keyof T
302307
? Defaults[K] extends undefined
303308
? T[K]

0 commit comments

Comments
 (0)