Skip to content

fix PropType #446

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 2 commits into from
Jul 20, 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
8 changes: 5 additions & 3 deletions src/component/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type ExtractFunctionPropType<
T extends Function,
TArgs extends Array<any> = any[],
TResult = any
> = T extends (...args: TArgs) => TResult ? T : any
> = T extends (...args: TArgs) => TResult ? T : never

type ExtractCorrectPropType<T> = T extends Function
? ExtractFunctionPropType<T>
Expand All @@ -58,8 +58,10 @@ type InferPropType<T> = T extends null
? { [key: string]: any }
: T extends BooleanConstructor | { type: BooleanConstructor }
? boolean
: T extends Prop<infer V>
? ExtractCorrectPropType<V> : T;
: T extends FunctionConstructor
? Function
: T extends Prop<infer V>
? ExtractCorrectPropType<V> : T;

export type ExtractPropTypes<
O,
Expand Down
15 changes: 15 additions & 0 deletions test-dts/defineComponent.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
reactive,
expectType,
expectError,
isNotAnyOrUndefined,
defineComponent,
PropType,
} from './index'
Expand Down Expand Up @@ -88,6 +89,20 @@ describe('with object props', () => {
expectType<ExpectedProps['fff']>(props.fff)
expectType<ExpectedProps['hhh']>(props.hhh)

isNotAnyOrUndefined(props.a)
isNotAnyOrUndefined(props.b)
isNotAnyOrUndefined(props.e)
isNotAnyOrUndefined(props.bb)
isNotAnyOrUndefined(props.cc)
isNotAnyOrUndefined(props.dd)
isNotAnyOrUndefined(props.ee)
isNotAnyOrUndefined(props.ff)
isNotAnyOrUndefined(props.ccc)
isNotAnyOrUndefined(props.ddd)
isNotAnyOrUndefined(props.eee)
isNotAnyOrUndefined(props.fff)
isNotAnyOrUndefined(props.hhh)

expectError((props.a = 1))

// setup context
Expand Down
5 changes: 5 additions & 0 deletions test-dts/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ export function describe(_name: string, _fn: () => void): void
export function expectType<T>(value: T): void
export function expectError<T>(value: T): void
export function expectAssignable<T, T2 extends T = T>(value: T2): void

// https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360
type IfNotAny<T> = 0 extends 1 & T ? never : T
type IfNotUndefined<T> = Exclude<T, undefined> extends never ? never : T
export function isNotAnyOrUndefined<T>(value: IfNotAny<IfNotUndefined<T>>): void