types: remove tuple check and add type check for tuple

This commit is contained in:
pikax 2020-04-15 16:22:44 +01:00
parent 70b55d797f
commit 8a74260b70
2 changed files with 7 additions and 24 deletions

View File

@ -128,31 +128,13 @@ export function toRef<T extends object, K extends keyof T>(
// RelativePath extends object -> true // RelativePath extends object -> true
type BaseTypes = string | number | boolean | Node | Window type BaseTypes = string | number | boolean | Node | Window
// Super simple tuple checker
type IsTuple<T extends Array<any>> = T[0] extends T[1]
? T[1] extends T[2] ? never : true
: true
export type UnwrapRef<T> = T extends ComputedRef<infer V> export type UnwrapRef<T> = T extends ComputedRef<infer V>
? UnwrapRefSimple<V> ? UnwrapRefSimple<V>
: T extends Ref<infer V> ? UnwrapRefSimple<V> : UnwrapRefSimple<T> : T extends Ref<infer V> ? UnwrapRefSimple<V> : UnwrapRefSimple<T>
type UnwrapRefSimple<T> = T extends type UnwrapRefSimple<T> = T extends Function | CollectionTypes | BaseTypes | Ref
| Function
| CollectionTypes
| BaseTypes
| Ref
| Element
? T ? T
: T extends Array<infer V> : T extends Array<any> ? T : T extends object ? UnwrappedObject<T> : T
? IsTuple<T> extends true ? UnwrapTuple<T> : Array<V>
: T extends object ? UnwrappedObject<T> : T
export type UnwrapTuple<T> = { [P in keyof T]: T[P] } & {
length: number
[Symbol.iterator]: any
[Symbol.unscopables]: any
}
// Extract all known symbols from an object // Extract all known symbols from an object
// when unwrapping Object the symbols are not `in keyof`, this should cover all the // when unwrapping Object the symbols are not `in keyof`, this should cover all the

View File

@ -21,14 +21,15 @@ function plainType(arg: number | Ref<number>) {
expectType<Ref<{ foo: number }>>(nestedRef) expectType<Ref<{ foo: number }>>(nestedRef)
expectType<{ foo: number }>(nestedRef.value) expectType<{ foo: number }>(nestedRef.value)
// tuple
expectType<[number, string]>(unref(ref([1, '1'])))
interface IteratorFoo { interface IteratorFoo {
[Symbol.iterator]: any [Symbol.iterator]: any
} }
expectType<Ref<UnwrapRef<IteratorFoo>> | Ref<null>>(
ref<IteratorFoo | null>(null)
)
expectType<Ref<HTMLElement> | Ref<null>>(ref<HTMLElement | null>(null)) // with symbol
expectType<IteratorFoo | null>(unref(ref<IteratorFoo | null>(null)))
} }
plainType(1) plainType(1)