Skip to content

Commit

Permalink
fix(types): Use alternative Omit
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmatter committed Oct 16, 2023
1 parent b8fc18c commit 91e54db
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
25 changes: 25 additions & 0 deletions packages/dts-test/setupHelpers.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,31 @@ describe('defineProps w/ union type declaration + withDefaults', () => {
)
})

describe('defineProps w/ generic discriminate union + withDefaults', () => {
interface B {
b?: string
}
interface S<T> extends B {
mode: 'single'
v: T
}
interface M<T> extends B {
mode: 'multiple'
v: T[]
}
type Props = S<string> | M<string>
const props = withDefaults(defineProps<Props>(), {
b: 'b'
})

if (props.mode === 'single') {
expectType<string>(props.v)
}
if (props.mode === 'multiple') {
expectType<string[]>(props.v)
}
})

describe('defineProps w/ generic type declaration + withDefaults', <T extends
number, TA extends {
a: string
Expand Down
7 changes: 6 additions & 1 deletion packages/runtime-core/src/apiSetupHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,16 @@ type InferDefault<P, T> =
| ((props: P) => T & {})
| (T extends NativeType ? T : never)

// https://github.com/vuejs/core/issues/9335
type OmitKeepDiscriminatedUnion<T, K extends keyof any> = T extends any
? Pick<T, Exclude<keyof T, K>>
: never

type PropsWithDefaults<
T,
Defaults extends InferDefaults<T>,
BKeys extends keyof T
> = Omit<T, keyof Defaults> & {
> = OmitKeepDiscriminatedUnion<T, keyof Defaults> & {
[K in keyof Defaults]-?: K extends keyof T
? Defaults[K] extends undefined
? T[K]
Expand Down

0 comments on commit 91e54db

Please sign in to comment.