Skip to content

Commit

Permalink
fix(runtime-core): withDefaults & union types
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmatter committed Sep 17, 2024
1 parent 9a36f2a commit ee315b6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
71 changes: 71 additions & 0 deletions packages-private/dts-test/setupHelpers.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,77 @@ describe('defineProps w/ generic type declaration + withDefaults', <T extends
expectType<boolean>(res.bool)
})

describe('defineProps w/union type', () => {
type PP =
| {
type: 'text'
mm: string
}
| {
type: 'number'
mm: number | null
}

const res = defineProps<PP>()
expectType<string | number | null>(res.mm)

if (res.type === 'text') {
expectType<string>(res.mm)
}

if (res.type === 'number') {
expectType<number | null>(res.mm)
}
})

describe('withDefaults w/ union type', () => {
type PP =
| {
type?: 'text'
mm: string
}
| {
type?: 'number'
mm: number | null
}

const res = withDefaults(defineProps<PP>(), {
type: 'text',
})

if (res.type && res.type === 'text') {
expectType<string>(res.mm)
}

if (res.type === 'number') {
expectType<number | null>(res.mm)
}
})

describe('withDefaults w/ generic union type', <T extends
| string
| number>() => {
type PP =
| {
tt?: 'a'
mm: T
}
| {
tt?: 'b'
mm: T[]
}

const res = withDefaults(defineProps<PP>(), {
tt: 'a',
})

if (res.tt === 'a') {
expectType<T>(res.mm)
} else {
expectType<T[]>(res.mm)
}
})

describe('withDefaults w/ boolean type', () => {
const res1 = withDefaults(
defineProps<{
Expand Down
5 changes: 1 addition & 4 deletions packages/runtime-core/src/apiSetupHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,6 @@ export function defineModel(): any {
}

type NotUndefined<T> = T extends undefined ? never : T
type MappedOmit<T, K extends keyof any> = {
[P in keyof T as P extends K ? never : P]: T[P]
}

type InferDefaults<T> = {
[K in keyof T]?: InferDefault<T, T[K]>
Expand All @@ -331,7 +328,7 @@ type PropsWithDefaults<
T,
Defaults extends InferDefaults<T>,
BKeys extends keyof T,
> = Readonly<MappedOmit<T, keyof Defaults>> & {
> = Readonly<T> & {
readonly [K in keyof Defaults as K extends keyof T
? K
: never]-?: K extends keyof T
Expand Down

0 comments on commit ee315b6

Please sign in to comment.