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(types): backport fix for withDefaults when used together with generics #13223

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions types/test/setup-helpers-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,40 @@ describe('defineProps w/ runtime declaration', () => {
props2.baz
})

describe('defineProps w/ generic type declaration + withDefaults', <T extends number, TA extends {
a: string
}, TString extends string>() => {
const res = withDefaults(
defineProps<{
n?: number
bool?: boolean

generic1?: T[] | { x: T }
generic2?: { x: T }
generic3?: TString
generic4?: TA
}>(),
{
n: 123,

generic1: () => [123, 33] as T[],
generic2: () => ({ x: 123 } as { x: T }),

generic3: () => 'test' as TString,
generic4: () => ({ a: 'test' } as TA)
}
)

res.n + 1

expectType<T[] | { x: T }>(res.generic1)
expectType<{ x: T }>(res.generic2)
expectType<TString>(res.generic3)
expectType<TA>(res.generic4)

expectType<boolean>(res.bool)
})

describe('defineEmits w/ type declaration', () => {
const emit = defineEmits<(e: 'change') => void>()
emit('change')
Expand Down
59 changes: 38 additions & 21 deletions types/v3-setup-helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,20 @@ export function defineProps<
PP extends ComponentObjectPropsOptions = ComponentObjectPropsOptions
>(props: PP): Readonly<ExtractPropTypes<PP>>
// overload 3: typed-based declaration
export function defineProps<TypeProps>(): Readonly<TypeProps>
export function defineProps<TypeProps>(): DefineProps<
TypeProps,
BooleanKey<TypeProps>
>

type DefineProps<T, BKeys extends keyof T> = Readonly<T> & {
readonly [K in BKeys]-?: boolean
}

type BooleanKey<T, K extends keyof T = keyof T> = K extends any
? [T[K]] extends [boolean | undefined]
? K
: never
: never

/**
* Vue `<script setup>` compiler macro for declaring a component's emitted
Expand Down Expand Up @@ -96,26 +109,26 @@ export function defineExpose<
type NotUndefined<T> = T extends undefined ? never : T

type InferDefaults<T> = {
[K in keyof T]?: InferDefault<T, NotUndefined<T[K]>>
[K in keyof T]?: InferDefault<T, T[K]>
}

type InferDefault<P, T> = T extends
| null
| number
| string
| boolean
| symbol
| Function
? T | ((props: P) => T)
: (props: P) => T
type NativeType = null | number | string | boolean | symbol | Function

type PropsWithDefaults<Base, Defaults> = Base & {
[K in keyof Defaults]: K extends keyof Base
? Defaults[K] extends undefined
? Base[K]
: NotUndefined<Base[K]>
: never
}
type InferDefault<P, T> =
| ((props: P) => T & {})
| (T extends NativeType ? T : never)

type PropsWithDefaults<
T,
Defaults extends InferDefaults<T>,
BKeys extends keyof T
> = Omit<T, keyof Defaults> & {
[K in keyof Defaults]-?: K extends keyof T
? Defaults[K] extends undefined
? T[K]
: NotUndefined<T[K]>
: never
} & { readonly [K in BKeys]-?: boolean }

/**
* Vue `<script setup>` compiler macro for providing props default values when
Expand All @@ -135,10 +148,14 @@ type PropsWithDefaults<Base, Defaults> = Base & {
* This is only usable inside `<script setup>`, is compiled away in the output
* and should **not** be actually called at runtime.
*/
export function withDefaults<Props, Defaults extends InferDefaults<Props>>(
props: Props,
export function withDefaults<
T,
BKeys extends keyof T,
Defaults extends InferDefaults<T>
>(
props: DefineProps<T, BKeys>,
defaults: Defaults
): PropsWithDefaults<Props, Defaults>
): PropsWithDefaults<T, Defaults, BKeys>

// make them global
type _defineProps = typeof defineProps
Expand Down