Skip to content

Commit fcee038

Browse files
authored
feat(types): fix props JSDoc loss (#935)
* feat(types): simplify `ExtractPropTypes` to avoid props JSDocs being removed * feat(types): avoid props JSDocs loss due to `default` option
1 parent 2ddd57f commit fcee038

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/component/componentProps.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ type InferPropType<T> = T extends null
6969
: ExtractCorrectPropType<V>
7070
: T
7171

72-
export type ExtractPropTypes<O> = O extends object
73-
? { [K in RequiredKeys<O>]: InferPropType<O[K]> } & {
74-
[K in OptionalKeys<O>]?: InferPropType<O[K]>
75-
}
76-
: { [K in string]: any }
72+
export type ExtractPropTypes<O> = {
73+
// use `keyof Pick<O, RequiredKeys<O>>` instead of `RequiredKeys<O>` to support IDE features
74+
[K in keyof Pick<O, RequiredKeys<O>>]: InferPropType<O[K]>
75+
} & {
76+
// use `keyof Pick<O, OptionalKeys<O>>` instead of `OptionalKeys<O>` to support IDE features
77+
[K in keyof Pick<O, OptionalKeys<O>>]?: InferPropType<O[K]>
78+
}
7779

7880
type DefaultKeys<T> = {
7981
[K in keyof T]: T[K] extends
@@ -93,5 +95,6 @@ type DefaultKeys<T> = {
9395

9496
// extract props which defined with default from prop options
9597
export type ExtractDefaultPropTypes<O> = O extends object
96-
? { [K in DefaultKeys<O>]: InferPropType<O[K]> }
98+
? // use `keyof Pick<O, DefaultKeys<O>>` instead of `DefaultKeys<O>` to support IDE features
99+
{ [K in keyof Pick<O, DefaultKeys<O>>]: InferPropType<O[K]> }
97100
: {}

0 commit comments

Comments
 (0)