Skip to content

Commit

Permalink
fix(types): backport fix from vuejs/core#8335
Browse files Browse the repository at this point in the history
  • Loading branch information
francis-switcho committed Nov 8, 2024
1 parent 9e88707 commit a56d621
Showing 1 changed file with 38 additions and 21 deletions.
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

0 comments on commit a56d621

Please sign in to comment.