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): inference for generic object types #9418

Closed
wants to merge 2 commits 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
19 changes: 18 additions & 1 deletion packages/dts-test/setupHelpers.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
defineSlots,
VNode,
Ref,
defineModel
defineModel,
PropType
} from 'vue'
import { describe, expectType } from './utils'
import { defineComponent } from 'vue'
Expand Down Expand Up @@ -40,6 +41,22 @@ describe('defineProps w/ generics', () => {
test()
})

// #9277
describe('defineProps w/ generic object type', <T>() => {
type Props<T> = {
modelValue: T
}

const props = defineProps({
modelValue: {
type: Object as PropType<Props<T>['modelValue']>,
required: true
}
})

expectType<T>(props.modelValue)
})

describe('defineProps w/ type declaration + withDefaults', () => {
const res = withDefaults(
defineProps<{
Expand Down
21 changes: 15 additions & 6 deletions packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import {
EMPTY_ARR,
def,
extend,
isOn,
IfAny
isOn
} from '@vue/shared'
import { warn } from './warning'
import {
Expand Down Expand Up @@ -122,10 +121,20 @@ 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] extends [{ type: infer U; default: infer D }]
? [T] extends [PropOptions<infer V, infer E>]
? V
: D
: [T] extends [{ type: infer U }]
? [T] extends [PropOptions<infer V>]
? V
: U
: [T] extends [{ default: infer U }]
? [T] extends [PropOptions<infer V, infer D>]
? D
: U
: [T] extends [PropType<infer V>]
? V
Comment on lines +124 to +137
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a few infers not being used can you remove any infers that are not being used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, will do

: T

/**
Expand Down