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): unexpected props type inference failed #1898

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
6 changes: 5 additions & 1 deletion packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ type DefaultFactory<T> = (props: Data) => T | null | undefined
interface PropOptions<T = any, D = T> {
type?: PropType<T> | true | null
required?: boolean
default?: D | DefaultFactory<D> | null | undefined
default?:
| D
| DefaultFactory<T extends (...arg: any) => any ? D : T>
| null
| undefined
validator?(value: unknown): boolean
}

Expand Down
6 changes: 6 additions & 0 deletions test-dts/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('with object props', () => {
hhh: boolean
ggg: 'foo' | 'bar'
validated?: string
fnWithDefault: (a: number) => number
}

type GT = string & { __brand: unknown }
Expand Down Expand Up @@ -93,6 +94,10 @@ describe('with object props', () => {
type: String,
// validator requires explicit annotation
validator: (val: unknown) => val !== ''
},
fnWithDefault: {
type: Function as PropType<(a: number) => number>,
default: (a: number) => a * 2
}
},
setup(props) {
Expand All @@ -113,6 +118,7 @@ describe('with object props', () => {
expectType<ExpectedProps['hhh']>(props.hhh)
expectType<ExpectedProps['ggg']>(props.ggg)
expectType<ExpectedProps['validated']>(props.validated)
expectType<ExpectedProps['fnWithDefault']>(props.fnWithDefault)

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