Skip to content

Commit

Permalink
types(props): Support undefined as default (#3783)
Browse files Browse the repository at this point in the history
  • Loading branch information
pikax authored May 17, 2021
1 parent 47da921 commit 92e7330
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type RequiredKeys<T> = {
// don't mark Boolean props as undefined
| BooleanConstructor
| { type: BooleanConstructor }
? K
? T[K] extends { default: undefined | (() => undefined) } ? never : K
: never
}[keyof T]

Expand Down
12 changes: 12 additions & 0 deletions test-dts/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ describe('with object props', () => {
h: boolean
bb: string
bbb: string
bbbb: string | undefined
bbbbb: string | undefined
cc?: string[] | undefined
dd: { n: 1 }
ee?: () => string
Expand Down Expand Up @@ -62,6 +64,14 @@ describe('with object props', () => {
// annotation
default: (props: any) => (props.bb as string) || 'foo'
},
bbbb: {
type: String,
default: undefined
},
bbbbb: {
type: String,
default: () => undefined
},
// explicit type casting
cc: Array as PropType<string[]>,
// required + type casting
Expand Down Expand Up @@ -129,6 +139,8 @@ describe('with object props', () => {
expectType<ExpectedProps['h']>(props.h)
expectType<ExpectedProps['bb']>(props.bb)
expectType<ExpectedProps['bbb']>(props.bbb)
expectType<ExpectedProps['bbbb']>(props.bbbb)
expectType<ExpectedProps['bbbbb']>(props.bbbbb)
expectType<ExpectedProps['cc']>(props.cc)
expectType<ExpectedProps['dd']>(props.dd)
expectType<ExpectedProps['ee']>(props.ee)
Expand Down

0 comments on commit 92e7330

Please sign in to comment.