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): extract the correct props type for the DateConstructor #2676

Merged
merged 2 commits into from
Feb 5, 2021
Merged
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
4 changes: 3 additions & 1 deletion packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ type InferPropType<T> = T extends null
? Record<string, any>
: T extends BooleanConstructor | { type: BooleanConstructor }
? boolean
: T extends Prop<infer V, infer D> ? (unknown extends V ? D : V) : T
: T extends DateConstructor | { type: DateConstructor }
? Date
: T extends Prop<infer V, infer D> ? (unknown extends V ? D : V) : T

export type ExtractPropTypes<O> = O extends object
? { [K in RequiredKeys<O>]: InferPropType<O[K]> } &
Expand Down
5 changes: 4 additions & 1 deletion test-dts/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('with object props', () => {
ggg: 'foo' | 'bar'
ffff: (a: number, b: string) => { a: boolean }
validated?: string
date?: Date
}

type GT = string & { __brand: unknown }
Expand Down Expand Up @@ -103,7 +104,8 @@ describe('with object props', () => {
type: String,
// validator requires explicit annotation
validator: (val: unknown) => val !== ''
}
},
date: Date
},
setup(props) {
// type assertion. See https://github.com/SamVerschueren/tsd
Expand All @@ -125,6 +127,7 @@ describe('with object props', () => {
expectType<ExpectedProps['ggg']>(props.ggg)
expectType<ExpectedProps['ffff']>(props.ffff)
expectType<ExpectedProps['validated']>(props.validated)
expectType<ExpectedProps['date']>(props.date)

// @ts-expect-error props should be readonly
expectError((props.a = 1))
Expand Down