Skip to content
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

types(reactivity): fix corner case on UnwrapRef #261

Merged
merged 1 commit into from
Mar 12, 2020
Merged
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
23 changes: 14 additions & 9 deletions src/reactivity/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import { reactive } from './reactive';

type BailTypes = Function | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any>;

// corner case when use narrows type
// Ex. type RelativePath = string & { __brand: unknown }
// RelativePath extends object -> true
type BaseTypes = string | number | boolean;

export interface Ref<T> {
value: T;
}
Expand All @@ -16,63 +21,63 @@ export interface Ref<T> {
// practical use cases...
export type UnwrapRef<T> = T extends Ref<infer V>
? UnwrapRef2<V>
: T extends BailTypes
: T extends BailTypes | BaseTypes
? T // bail out on types that shouldn't be unwrapped
: T extends object ? { [K in keyof T]: UnwrapRef2<T[K]> } : T

// prettier-ignore
type UnwrapRef2<T> = T extends Ref<infer V>
? UnwrapRef3<V>
: T extends BailTypes
: T extends BailTypes | BaseTypes
? T
: T extends object ? { [K in keyof T]: UnwrapRef3<T[K]> } : T

// prettier-ignore
type UnwrapRef3<T> = T extends Ref<infer V>
? UnwrapRef4<V>
: T extends BailTypes
: T extends BailTypes | BaseTypes
? T
: T extends object ? { [K in keyof T]: UnwrapRef4<T[K]> } : T

// prettier-ignore
type UnwrapRef4<T> = T extends Ref<infer V>
? UnwrapRef5<V>
: T extends BailTypes
: T extends BailTypes | BaseTypes
? T
: T extends object ? { [K in keyof T]: UnwrapRef5<T[K]> } : T

// prettier-ignore
type UnwrapRef5<T> = T extends Ref<infer V>
? UnwrapRef6<V>
: T extends BailTypes
: T extends BailTypes | BaseTypes
? T
: T extends object ? { [K in keyof T]: UnwrapRef6<T[K]> } : T

// prettier-ignore
type UnwrapRef6<T> = T extends Ref<infer V>
? UnwrapRef7<V>
: T extends BailTypes
: T extends BailTypes | BaseTypes
? T
: T extends object ? { [K in keyof T]: UnwrapRef7<T[K]> } : T

// prettier-ignore
type UnwrapRef7<T> = T extends Ref<infer V>
? UnwrapRef8<V>
: T extends BailTypes
: T extends BailTypes | BaseTypes
? T
: T extends object ? { [K in keyof T]: UnwrapRef8<T[K]> } : T

// prettier-ignore
type UnwrapRef8<T> = T extends Ref<infer V>
? UnwrapRef9<V>
: T extends BailTypes
: T extends BailTypes | BaseTypes
? T
: T extends object ? { [K in keyof T]: UnwrapRef9<T[K]> } : T

// prettier-ignore
type UnwrapRef9<T> = T extends Ref<infer V>
? UnwrapRef10<V>
: T extends BailTypes
: T extends BailTypes | BaseTypes
? T
: T extends object ? { [K in keyof T]: UnwrapRef10<T[K]> } : T

Expand Down