Skip to content

Commit 4414372

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

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

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

+34-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import {
2-
defineProps,
2+
Ref,
3+
Slots,
4+
VNode,
5+
defineComponent,
36
defineEmits,
7+
defineModel,
8+
defineProps,
9+
defineSlots,
410
useAttrs,
11+
useModel,
512
useSlots,
6-
withDefaults,
7-
Slots,
8-
defineSlots,
9-
VNode,
10-
Ref,
11-
defineModel
13+
withDefaults
1214
} from 'vue'
1315
import { describe, expectType } from './utils'
14-
import { defineComponent } from 'vue'
15-
import { useModel } from 'vue'
1616

1717
describe('defineProps w/ type declaration', () => {
1818
// type declaration
@@ -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

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
import { Ref, ref } from '@vue/reactivity'
12
import {
2-
isArray,
3-
isPromise,
4-
isFunction,
53
Prettify,
64
UnionToIntersection,
7-
extend
5+
extend,
6+
isArray,
7+
isFunction,
8+
isPromise
89
} from '@vue/shared'
10+
import { watch } from './apiWatch'
911
import {
10-
getCurrentInstance,
11-
setCurrentInstance,
1212
SetupContext,
1313
createSetupContext,
14+
getCurrentInstance,
15+
setCurrentInstance,
1416
unsetCurrentInstance
1517
} from './component'
1618
import { EmitFn, EmitsOptions, ObjectEmitsOptions } from './componentEmits'
@@ -21,16 +23,14 @@ import {
2123
MethodOptions
2224
} from './componentOptions'
2325
import {
24-
ComponentPropsOptions,
2526
ComponentObjectPropsOptions,
27+
ComponentPropsOptions,
2628
ExtractPropTypes,
2729
NormalizedProps,
2830
PropOptions
2931
} from './componentProps'
30-
import { warn } from './warning'
3132
import { SlotsType, StrictUnwrapSlotsType } from './componentSlots'
32-
import { Ref, ref } from '@vue/reactivity'
33-
import { watch } from './apiWatch'
33+
import { warn } from './warning'
3434

3535
// dev only
3636
const warnRuntimeUsage = (method: string) =>
@@ -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)