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): incomplete type inferred by generic components #9652

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
20 changes: 20 additions & 0 deletions packages/dts-test/setupHelpers.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
useAttrs,
useSlots,
withDefaults,
PropType,
Slots,
defineSlots,
VNode,
Expand Down Expand Up @@ -193,6 +194,25 @@ describe('defineProps w/ runtime declaration', () => {
props2.baz
})

describe('defineProps w/ generics and runtime declarations', () => {
function test<T extends Record<string, any>>() {
const props = defineProps({
foo: {
type: Object as PropType<T>,
required: false,
default: null
},
bar: {
type: Object as PropType<T>,
required: true
}
})
expectType<T | null>(props.foo)
expectType<T>(props.bar)
}
test()
})

describe('defineEmits w/ type declaration', () => {
const emit = defineEmits<(e: 'change') => void>()
emit('change')
Expand Down
16 changes: 11 additions & 5 deletions packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,17 @@ type InferPropType<T> = [T] extends [null]
? U extends DateConstructor
? Date | InferPropType<U>
: InferPropType<U>
: [T] extends [Prop<infer V, infer D>]
? unknown extends V
? IfAny<V, V, D>
: V
: T
: [T] extends [PropOptions<infer V, infer D>]
? unknown extends D
? V
: unknown extends V
? IfAny<V, V, D>
: V
: [T] extends [Prop<infer V, infer D>]
? unknown extends V
? IfAny<V, V, D>
: V
: T

/**
* Extract prop types from a runtime props options object.
Expand Down