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 f6aea2f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 21 deletions.
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

0 comments on commit f6aea2f

Please sign in to comment.