Skip to content

reapply #352 and fix test: Remove Function type from PropType type #371

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

Merged
merged 1 commit into from
Jun 12, 2020
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
13 changes: 12 additions & 1 deletion src/component/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ type RequiredKeys<T, MakeDefaultRequired> = {

type OptionalKeys<T, MakeDefaultRequired> = Exclude<keyof T, RequiredKeys<T, MakeDefaultRequired>>;

type ExtractFunctionPropType<
T extends Function,
TArgs extends Array<any> = any[],
TResult = any
> = T extends (...args: TArgs) => TResult ? T : never;

type ExtractCorrectPropType<T> = T extends Function
? ExtractFunctionPropType<T>
: Exclude<T, Function>;

// prettier-ignore
type InferPropType<T> = T extends null
? any // null & true would fail to infer
Expand All @@ -43,7 +53,8 @@ type InferPropType<T> = T extends null
? { [key: string]: any }
: T extends BooleanConstructor | { type: BooleanConstructor }
? boolean
: T extends Prop<infer V> ? V : T;
: T extends Prop<infer V>
? ExtractCorrectPropType<V> : T;

export type ExtractPropTypes<O, MakeDefaultRequired extends boolean = true> = O extends object
? { [K in RequiredKeys<O, MakeDefaultRequired>]: InferPropType<O[K]> } &
Expand Down
2 changes: 1 addition & 1 deletion test/types/defineComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('defineComponent', () => {
},
setup(props) {
type PropsType = typeof props;
isSubType<{ user?: User }, PropsType>(true);
isSubType<{ user?: User; func?: () => boolean; userFunc?: (u: User) => User }, PropsType>(true);
isSubType<PropsType, { user?: User; func?: () => boolean; userFunc?: (u: User) => User }>(
true
);
Expand Down