Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): withDefaults & union types #11949

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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