Skip to content

Commit

Permalink
feat(types): fix props JSDoc loss (#935)
Browse files Browse the repository at this point in the history
* feat(types): simplify `ExtractPropTypes` to avoid props JSDocs being removed

* feat(types): avoid props JSDocs loss due to `default` option
  • Loading branch information
xiaoxiangmoe authored May 18, 2022
1 parent 2ddd57f commit fcee038
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/component/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ type InferPropType<T> = T extends null
: ExtractCorrectPropType<V>
: T

export type ExtractPropTypes<O> = O extends object
? { [K in RequiredKeys<O>]: InferPropType<O[K]> } & {
[K in OptionalKeys<O>]?: InferPropType<O[K]>
}
: { [K in string]: any }
export type ExtractPropTypes<O> = {
// use `keyof Pick<O, RequiredKeys<O>>` instead of `RequiredKeys<O>` to support IDE features
[K in keyof Pick<O, RequiredKeys<O>>]: InferPropType<O[K]>
} & {
// use `keyof Pick<O, OptionalKeys<O>>` instead of `OptionalKeys<O>` to support IDE features
[K in keyof Pick<O, OptionalKeys<O>>]?: InferPropType<O[K]>
}

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

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

0 comments on commit fcee038

Please sign in to comment.