Options
All
  • Public
  • Public/Protected
  • All
Menu

Index

Type aliases

Add<A, B>: [...NumberToTupleLength<A>, ...NumberToTupleLength<B>]["length"]
name

Add

description

请实现Add<A, B>计算正整数之和。

example

type A = Add<1, 2> // 3

example

type B = Add<0, 0> // 0

Type parameters

  • A: number

  • B: number

CreateNumberObject<T, N, F>: N extends true ? F extends true ? NumberObjectByNF<T> : NumberObjectByN<T> : F extends true ? NumberObjectByPF<T> : NumberObjectByP<T>

Type parameters

Divide<A, B>: A extends 0 ? 0 : B extends 0 ? never : LargerThan2<A, B> extends false ? 0 : [A] extends [B] ? 1 : DivideHelper<A, B>
name

Divide

description

Implement Divide<A, B> Math.floow(A/B)

description

只需要考虑非负整数。

example

type A = Divide<1, 0> // never

example

type B = Divide<4, 2> // 2

example

type C = Divide<10, 3> // 3

example

type D = Divide<3, 3> // 1

example

type E = Divide<3, 4> // 0

Type parameters

  • A: number

  • B: number

DivideHelper<A, B, C>: 0 extends A ? C["length"] : LargerThan2<A, B> extends true ? DivideHelper<SubtractHelper1<A, B>, B, [...C, 1]> : C["length"]
name

DivideHelper

description

Divide 的辅助type,A.length肯定比B.length大

Type parameters

  • A: number

  • B: number

  • C: any[] = []

Equal<A, B>: true extends IsAny1<A> ? true extends IsAny1<B> ? true : false : true extends IsAny1<B> ? false : [A] extends [B] ? [B] extends [A] ? true : false : false
name

Equal

description

Implement Equal<A, B> to check if two types are identical.

example

type a = Equal<any, any> // true

example

type b = Equal<any, 1> // false

example

type c = Equal<never, never> // true

example

type d = Equal<'BFE', 'BFE'> // true

example

type e = Equal<'BFE', string> // false

example

type f = Equal<1 | 2, 2 | 1> // true

example

type g = Equal<{a : number}, {a: number}> // true

Type parameters

  • A

  • B

Filter<T, A, TT>: T extends [infer L, ...infer R] ? Filter<R, A, [L] extends [A] ? [...TT, L] : TT> : TT
name

Filter

description

请实现Filter<T, A>,返回T中能够assign给A的type所组成的新tuple。

example

type A = Filter<[1, 'BFE', 2, true, 'dev'], number> // [1, 2]

example

type B = Filter<[1, 'BFE', 2, true, 'dev'], string> // ['BFE', 'dev']

example

type C = Filter<[1, 'BFE', 2, true, 'dev'], null> // []

example

type D = Filter<[1, 'BFE', 2, any, 'dev'], string> // ['BFE', any, 'dev']

example

type E = Filter<[1, 'BFE', 2, any, 'dev'], number> // [1, 2, any]

将持续关注[L]-extends-[A]和L-extends-A的区别是啥

Type parameters

  • T: any[]

  • A

  • TT: any[] = []

FindIndex<T, E, TT>: T extends [infer L, ...infer R] ? [E] extends [L] ? IsAny1<L> extends IsAny1<E> ? TT["length"] : FindIndex<R, E, [...TT, 1]> : FindIndex<R, E, [...TT, 1]> : never
name

FindIndex

description

正如Array.prototype.findIndex(), 请实现FindIndex<T, E>。

example

type A = [any, never, 1, '2', true]

example

type B = FindIndex<A, 1> // 2

example

type C = FindIndex<A, 3> // never

Type parameters

  • T: any[]

  • E

  • TT: any[] = []

FirstChar<T>: T extends `${infer P}${any}` ? P : never

Type parameters

  • T: string

FirstItem<T>: T extends [infer F, ...any] ? F : never
name

FirstItem

description

和FirstChar类似,实现FirstItem来返回tuple type的第一个type。

example

FirstItem<[string, number, boolean]> // string

example

FirstItem<['B', 'F', 'E']> // 'B'

Type parameters

  • T: any[]

Flat<T, AT>: T extends [infer L, ...infer R] ? L extends any[] ? [...Flat<L>, ...Flat<R>] : [L, ...Flat<R>] : AT
name

Flat

description

实现Flat扁平化元组类型。 - 清晰明了

example

type A = Flat<[1,2,3]> // [1,2,3]

example

type B = Flat<[1,[2,3], [4,[5,[6]]]]> // [1,2,3,4,5,6]

example

type C = Flat<[]> // []

Type parameters

  • T: any[]

  • AT: any[] = []

Flat1<T, AT>: T extends [infer L, ...infer R] ? [...L extends any[] ? Flat<L> : [L], ...Flat<R>] : AT
name

Flat1

description

实现Flat1扁平化元组类型。 - 简单

example

type A = Flat1<[1,2,3]> // [1,2,3]

example

type B = Flat1<[1,[2,3], [4,[5,[6]]]]> // [1,2,3,4,5,6]

example

type C = Flat1<[]> // []

Type parameters

  • T: any[]

  • AT: any[] = []

FlatTupleToString<T, Return>: T extends [`${infer L}`, ...infer R] ? FlatTupleToString<R, `${L}${Return}`> : Return
name

FlatTupleToString

description

string[]元组类型转string

example

type A = FlatTupleToString<['a', 'b']> // 'ab'

Type parameters

  • T: unknown[]

  • Return: string = ""

IsAny<T>: (string | number | symbol) extends keyof T ? {} extends T ? true : false : false
name

IsAny

description

判断是否是any - 自己写的

example

type A = IsAny // false

example

type B = IsAny // true

example

type C = IsAny // false

example

type D = IsAny // false

Type parameters

  • T

IsAny1<T>: 0 extends 1 & T ? true : false
name

IsAny1

description

判断是否是any - 看答案的

example

type A = IsAny1 // false

example

type B = IsAny1 // true

example

type C = IsAny1 // false

example

type D = IsAny1 // false

0-extends-1不满足类型约束(0不可以分配给1),因此也不可能被(0-extends-(1&T))满足,因为1&T应该比T更窄。 any几乎可以匹配所有的其他类型,唯一不匹配的类型是:any-never

Type parameters

  • T

IsEmptyType<T>: number extends T ? keyof T extends never ? T extends {} ? true : false : false : false
name

IsEmptyType

description

Implement IsEmptyType to check if T is empty type {}.

Example

type A = IsEmptyType<string> // false
type B = IsEmptyType<{ a: 3 }> // false
type C = IsEmptyType<{}> // true
type D = IsEmptyType<any> // false
type E = IsEmptyType<object> // false
type F = IsEmptyType<Object> // false
type G = IsEmptyType<unknown> // false
type H = IsEmptyType<{ number: 3 }> // false

注意string['number']这个情况,即在keyof-T上有numbsr

Type parameters

  • T

IsFractional<T>: `${T}` extends `${string}.${string}` ? true : false

Type parameters

  • T: number

IsNegative<T>: `${T}` extends `-${string}` ? true : false

Type parameters

  • T: number

IsNever<T>: [T] extends [never] ? true : false
name

IsNever

description

请实现IsNever用以判断T是否是never。

example

type A = IsNever // true

example

type B = IsNever // false

example

type C = IsNever // false

通常,分配性是期望的行为。extends为避免这种行为,您可以用方括号将关键字的每一侧括起来。

Type parameters

  • T

IsObject<T>: [T] extends [object] ? true : false

判定是否对象类型

Type parameters

  • T

IsStringArray<T>: T extends infer I[] ? I extends string ? T : never : never

Type parameters

  • T: unknown

LTrim<T>: T extends `${infer L}${infer R}` ? [L] extends [" "] ? LTrim<R> : T : T
name

LTrim

description

String.prototype.trimStart

example

type A = LTrim<' xxx '> // 'xxx '

Type parameters

  • T: string

LargerThan<A, B, TT>: TT["length"] extends A ? false : TT["length"] extends B ? true : LargerThan<A, B, [...TT, 1]>
name

LargerThan

description

不考虑负数和小数的 LargerThan

example

type A = LargerThan<0, 1> // false

example

type B = LargerThan<1, 0> // true

example

type C = LargerThan<10, 9> // true

Type parameters

  • A: number

  • B: number

  • TT: number[] = []

LargerThan1<A, B, AO, BO, S, I, F>: S extends Failure ? false : S extends Victory ? true : AO["symbol"] extends 1 ? I extends Failure ? true : I extends Victory ? false : F extends Failure ? true : false : I extends Failure ? false : I extends Victory ? true : F extends Victory ? true : false
name

LargerThan1

description

判断number大于铁定是要带上小数和负数咯

example

type A = LargerThan1<0, 1> // false

example

type B = LargerThan1<1, 0> // true

example

type C = LargerThan1<10, 9> // true

example

type D = LargerThan1<10, 9.3> // true

example

type D1 = LargerThan1<9999999999.3, 99999999999.2>

example

type D2 = LargerThan1<-10, 9.3>

example

type D3 = LargerThan1<-9.34, -9.32>

example

type D4 = LargerThan1<9.33, 9.323>

example

type D5 = LargerThan1<-9.33232435455, -9.312838435934953>

example

type D6 = LargerThan1<-9.33213123123, -9.32>

Type parameters

LargerThan2<A, B, TT>: TT["length"] extends A ? TT["length"] extends B ? true : false : TT["length"] extends B ? true : LargerThan2<A, B, [...TT, 1]>
name

LargerThan2

description

A大于等于B

example

type G1 = LargerThan<1, 2> // false

example

type G2 = LargerThan<1, 1> // true

example

type G3 = LargerThan<2, 1> // true

Type parameters

  • A: number

  • B: number

  • TT: number[] = []

LargerThan3<A, B>: NumberToTuple<A> extends [...NumberToTuple<B>, ...infer R] ? R["length"] extends 0 ? false : true : false
name

LargerThan3

description

A大于B

example

type G1 = LargerThan3<1, 2> // false

example

type G2 = LargerThan3<1, 1> // false

example

type G3 = LargerThan3<2, 1> // true

Type parameters

  • A: number

  • B: number

LastChar1<T>: T extends `${infer L}${infer R}` ? R extends "" ? L : LastChar1<R> : never

Type parameters

  • T: string

LastChar2<T, Right>: T extends `${infer L}${infer R}` ? LastChar2<R, L> : Right

Type parameters

  • T: string

  • Right = never

LastItem<T>: T extends [...any, infer R] ? R : never
name

LastItem

description

和 FirstItem 类似,实现 LastItem 用以返回tuple的最后一个type。

example

type A = LastItem<[string, number, boolean]> // boolean

example

type B = LastItem<['B', 'F', 'E']> // 'E'

example

type C = LastItem<[]> // never

Type parameters

  • T: any[]

LengthOfString<T, AT>: T extends `${infer L}${infer R}` ? LengthOfString<R, [...AT, L]> : AT["length"]
name

LengthOfString

description

请实现LengthOfString用以返回字符串长度。

example

type A = LengthOfString<'BFE.dev'> // 7

example

type B = LengthOfString<''> // 0

通过string转array来实现

Type parameters

  • T: string

  • AT: any[] = []

LengthOfTuple<T>: "length" extends keyof T ? T["length"] : 0
name

LengthOfTuple

description

请实现LengthOfTuple返回tuple type的长度。

example

type A = LengthOfTuple<['B', 'F', 'E']> // 3

example

type B = LengthOfTuple<[]> // 0

Type parameters

  • T: any[]

Multiply<A, B>: 0 extends A ? 0 : 0 extends B ? 0 : MultiplyHelper<NumberToTuple<A>, NumberToTuple<B>>
name

Multiply

description

Implement Multiply<A, B> A * B, 正整数乘法

example

type A = Multiply<1, 0> // 0

example

type B = Multiply<4, 6> // 24

Type parameters

  • A: number

  • B: number

MultiplyHelper<A, B, C, D>: C["length"] extends A["length"] ? D["length"] : MultiplyHelper<A, B, [...C, any], [...D, ...B]>

Type parameters

  • A: any[]

  • B: any[]

  • C: any[] = []

  • D: any[] = []

MyConstructorParameters<T>: T extends abstract new (...args: infer P) => any ? P : never

Type parameters

  • T: abstract new (...args: any) => any

MyExclude<T, K>: T extends K ? never : T

Type parameters

  • T

  • K

MyExtract<T, U>: T extends U ? T : never

Type parameters

  • T

  • U

MyInstanceType<T>: T extends abstract new (...args: any) => infer P ? P : T
description

构造函数返回的是类的实例,具体可参考 new 的实现

Type parameters

  • T: abstract new (...args: any) => any

MyNonNullable<T>: T extends null | undefined ? never : T

Type parameters

  • T

MyOmit<T, K>: { [ P in MyExclude<keyof T, K>]: T[P] }

Type parameters

  • T

  • K: string | number | symbol

MyOmitThisParameter<T>: unknown extends ThisParameterType<T> ? T : T extends (...args: infer P) => infer R ? (...args: P) => R : T

Type parameters

  • T

MyParameters<T>: T extends (...args: infer U) => unknown ? U : never

Type parameters

  • T: (...args: any) => any

MyPartial<T>: { [ P in keyof T]?: T[P] }

Type parameters

  • T

MyPick<T, K>: { [ P in K]: T[P] }

Type parameters

  • T

  • K: keyof T

MyReadonly<T>: { readonly [ P in keyof T]: T[P] }

Type parameters

  • T

MyRecord<K, V>: { [ P in K]: V }

Type parameters

  • K: string | number | symbol

  • V

MyRequired<T>: { [ P in keyof T]-?: T[P] }
description

映射类型 「+」 「-」 用来影响 「readonly」 和 「?」 属性 {只读、可变}

Type parameters

  • T

MyReturnType<T>: T extends (...args: any) => infer P ? P : never

Type parameters

  • T: (...args: any) => any

MyThisParameterType<T>: T extends (this: infer P, ...args: any) => any ? P : unknown

Type parameters

  • T

NSymbol: 0 | 1
NumberArrayCompare<A, B, CL, LR>: CL extends true ? LR extends Tie ? NumberArrayCompareHandler<A, B> : LR : NumberArrayCompareHandler<A, B>

Type parameters

  • A: string[]

  • B: string[]

  • CL: boolean

  • LR = NumberCompare<`${A["length"]}`, `${B["length"]}`>

NumberArrayCompareHandler<A, B>: A["length"] extends 0 ? B["length"] extends 0 ? Tie : Failure : NumberCompare<Pop<A>, Pop<B>> extends Tie ? NumberArrayCompareHandler<IsStringArray<ShiftString<A>>, IsStringArray<ShiftString<B>>> : NumberCompare<Pop<A>, Pop<B>>

Type parameters

  • A: string[]

  • B: string[]

NumberCompare<A, B, T, L>: L extends A ? L extends B ? Tie : Failure : L extends B ? Victory : NumberCompare<A, B, [...T, "_"]>

Type parameters

  • A: string

  • B: string

  • T: string[] = []

  • L = `${T["length"]}`

NumberObjectByN<T>: `${T}` extends `${infer _}${infer I}` ? NumberObject<1, Split<I>, []> : never

Type parameters

  • T: number

NumberObjectByNF<T>: `${T}` extends `${infer _}${infer I}.${infer F}` ? NumberObject<1, Split<I>, Split<F>> : never

Type parameters

  • T: number

NumberObjectByP<T>: NumberObject<0, Split<`${T}`>, []>

Type parameters

  • T: number

NumberObjectByPF<T>: `${T}` extends `${infer I}.${infer F}` ? NumberObject<0, Split<I>, Split<F>> : never

Type parameters

  • T: number

NumberToTuple<T, R>: R["length"] extends T ? R : NumberToTuple<T, [...R, any]>
name

NumberToTuple

descript

number 转 tuple类型

example

type A = NumberToTuple<0> // []

example

type A = NumberToTuple<1> // [any]

example

type A = NumberToTuple<2> // [any, any]

Type parameters

  • T: number = 0

  • R: any[] = []

NumberToTupleLength<T, TT>: [T] extends [TT["length"]] ? TT : NumberToTupleLength<T, [...TT, any]>
name

NumberToTupleLength

description

把正整数number转成一个any类型的元组

example

type A = NumberToTupleLength<1> // [any]

example

type B = NumberToTupleLength<2> // [any, any]

example

type C = NumberToTupleLength<3> // [any, any, any]

Type parameters

  • T: number

  • TT: any[] = []

Pop<T>: T[0]

Type parameters

  • T: string[]

Push<T, I>: T extends [...infer Rest] ? [...Rest, I] : T
name

Push

description

实现Push<T, I>

example

type A = Push<[1,2,3], 4> // [2,3]

example

type B = Push<[1], 2> // [1, 2]

example

type C = Push<[], string> // [string]

Type parameters

  • T: any[]

  • I

RTrim<T>: T extends `${infer L}${infer R}` ? [R] extends [" "] ? L : [RTrim<R>] extends [" "] ? L : `${L}${RTrim<R>}` : T
name

RTrim

description

String.prototype.trimEnd

example

type A = LTrim<' xxx '> // ' xxx'

Type parameters

  • T: string

Repeat<T, C, AT>: C extends AT["length"] ? AT : Repeat<T, C, [...AT, T]>
name

Repeat

description

Implement Repeat<T, C> to return a tuple by repeating.

example

type A = Repeat<number, 3> // [number, number, number]

example

type B = Repeat<string, 2> // [string, string]

example

type C = Repeat<1, 1> // [1, 1]

example

type D = Repeat<0, 0> // []

Type parameters

  • T

  • C: number

  • AT: T[] = []

RepeatString<T, C, R>: C extends R["length"] ? FlatTupleToString<R> : RepeatString<T, C, [...R, T]>
name

RepeatString

description

类似于String.prototype.repeat(),请实现RepeatString<T, C>。

example

type A = RepeatString<'a', 3> // 'aaa'

example

type B = RepeatString<'a', 0> // ''

example

type C = RepeatString<'abc', 0> // 'abcabcabc'

看到了很多其他的思路,有先把string转换成tuple的,还有利用一个tuple去做递归不断的扩展length的,我看到了许多思想的迸发,That's-Good!

Type parameters

  • T: string

  • C: number

  • R: T[] = []

RepeatString1<T, C, A>: A["length"] extends C ? "" : `${T}${RepeatString1<T, C, [T, ...A]>}`
name

RepeatString1

description

类似于String.prototype.repeat(),请实现RepeatString1<T, C>。

example

type A = RepeatString1<'a', 3> // 'aaa'

example

type B = RepeatString1<'a', 0> // ''

example

type C = RepeatString1<'abc', 0> // 'abcabcabc'

Type parameters

  • T: string

  • C: number

  • A: string[] = []

ReplaceAll<S, F, T>: F extends "" ? S : S extends `${infer A}${F}${infer B}` ? `${A}${T}${ReplaceAll<B, F, T>}` : S
name

ReplaceAll

description

Just like String.prototype.replaceAll(),

example

type A = ReplaceAll<'aba', 'b', ''> // 'aa'

example

type B = ReplaceAll<'ababbab', 'b', ''> // 'aaa'

example

type B = ReplaceAll<'ababbab', 'b', ''> // 'aaa'

Type parameters

  • S: string

  • F: string

  • T: string

ReverseTuple<T, AT>: T extends [infer L, ...infer R] ? ReverseTuple<R, [L, ...AT]> : AT
name

ReverseTuple

description

实现ReverseTuple反转元组类型。

example

type A = ReverseTuple<[string, number, boolean]> // [boolean, number, string]

example

type B = ReverseTuple<[1,2,3]> // [3,2,1]

example

type C = ReverseTuple<[]> // []

Type parameters

  • T: any[]

  • AT: any[] = []

Shift<T>: T extends [any, ...infer Rest] ? Rest : []
name

Shift

description

请实现Shift来去掉tuple的第一个元素

example

type A = Shift<[1, 2, 3]> // [2, 3]

example

type B = Shift<[1]> // []

example

type C = Shift<[]> // []

Type parameters

  • T: any[]

ShiftString<T>: T extends [infer _, ...infer R] ? R : never

Type parameters

  • T: string[]

Slice<A, S, E, C, D, ST, ET>: [0] extends [A["length"]] ? D : [ST["length"]] extends [C["length"]] ? [ET["length"]] extends [C["length"]] ? D : A extends [infer L, ...infer R] ? Slice<R, S, E, [...C, 1], [...D, L], [...C, 1], ET> : D : A extends [infer L, ...infer R] ? Slice<R, S, E, [...C, 1], D, ST, ET> : D
name

Slice

description

Just like what Array.prototype.slice() does, please implement Slice<A, S, E>.

example

type A = Slice<[1,2,3,4], 0, 2> // [1, 2]

example

type B = Slice<[1,2,3,4], 2> // [3, 4]

example

type C = Slice<[number, boolean, bigint], 2, 5> // [bigint]

example

type D = Slice<[string, boolean], 0, 1> // [string]

example

type E = Slice<[number, boolean, bigint], 5, 6> // []

可以使用LessThan来比较大小,由于索引都是大于等于0的整数,所以可以使用简版的LessThan

Type parameters

SmallerThan<A, B>: NumberToTuple<A> extends [...NumberToTuple<B>, ...infer R] ? false : true
name

SmallerThan

description

实现小于,「A和B是大于等于0的整数」

example

type A = SmallerThan<0, 1> // true

example

type B = SmallerThan<1, 0> // false

example

type C = SmallerThan<10, 9> // false

example

type D = SmallerThan<0, 0> // false

Type parameters

  • A: number

  • B: number

Split<T, P>: T extends "" ? P : T extends `${infer F}${infer R}` ? Split<R, [...P, F]> : never

Type parameters

  • T: string

  • P: string[] = []

StringToTuple<T, Tuple>: T extends `${infer L}${infer R}` ? StringToTuple<R, [...Tuple, L]> : Tuple
name

StringToTuple

description

请实现StringToTuple将字符串拆散为tuple。

example

type A = StringToTuple<'BFE.dev'> // ['B', 'F', 'E', '.', 'd', 'e','v']

example

type B = StringToTuple<''> // []

Type parameters

  • T: string

  • Tuple: any[] = []

Subtract<A, B>: LargerThan2<A, B> extends false ? never : SubtractHelper<NumberToTuple<A>, NumberToTuple<B>>
name

Subtract

description

Similar to 38. implement Add<A, B>, please implement Subtract<A, B>.

description

1.only need to consider positive integers

description

2.B is guaranteed to be smaller or equal to A

example

type A = Subtract<1, 1> // 0

example

type B = Subtract<10, 3> // 7

example

type C = Subtract<3, 10> // never

Type parameters

  • A: number

  • B: number

Subtract1<A, B>: LargerThan2<A, B> extends false ? never : SubtractHelper1<A, B>
name

Subtract1

description

Similar to 38. implement Add<A, B>, please implement Subtract1<A, B>.

description

1.only need to consider positive integers

description

2.B is guaranteed to be smaller or equal to A

example

type A = Subtract1<1, 1> // 0

example

type B = Subtract1<10, 3> // 7

example

type C = Subtract1<3, 10> // never

Type parameters

  • A: number

  • B: number

SubtractHelper<A, B, R>: B["length"] extends A["length"] ? R["length"] : SubtractHelper<A, [...B, 1], [...R, 1]>
name

SubtractHelper

description

用来帮助计算 Subtract,前提条件:A.length肯定大于B.length

Type parameters

  • A: any[]

  • B: any[]

  • R: any[] = []

SubtractHelper1<A, B>: NumberToTuple<A> extends [...NumberToTuple<B>, ...infer R] ? R["length"] : never
name

SubtractHelper

description

用来帮助计算 Subtract1,前提条件:A肯定大于B

Type parameters

  • A: number

  • B: number

SymbolCompare<A, B>: SymbolCompareResMap[`${A}${B}`]

Type parameters

  • A: 0 | 1

  • B: 0 | 1

SymbolCompareResMap: { 00: Tie; 01: Victory; 10: Failure; 11: Tie }

Type declaration

ToNumber<T, TT>: `${TT["length"]}` extends T ? TT["length"] : ToNumber<T, [...TT, any]>
name

ToNumber

description

string 转换为 number

example

type A = ToNumber<'1'> // 1

example

type B = ToNumber<'40'> // 40

example

type C = ToNumber<'0'> // 0

Type parameters

  • T: string

  • TT: any[] = []

Trim<T>: LTrim<RTrim<T>>
name

Trim

description

正如String.prototype.trim(),请实现Trim

example

type A = Trim<' BFE.dev'> // 'BFE'

example

type B = Trim<' BFE. dev '> // 'BFE. dev'

example

type C = Trim<' BFE . dev '> // 'BFE . dev'

Type parameters

  • T: string

TupleToString<T>: T extends [infer L, ...infer R] ? `${L extends string ? L : ""}${R extends string[] ? TupleToString<R> : ""}` : ""
name

TupleToString

description

TupleToString通过将所有字符串连接到新的字符串类型来实现返回新类型。

example

type A = TupleToString<['a']> // 'a'

example

type B = TupleToString<['B', 'F', 'E']> // 'BFE'

example

type C = TupleToString<[]> // ''

类型收窄要常用extends+条件判断语句

Type parameters

  • T: string[]

TupleToUnion<T, R>: T extends [infer P, ...infer Q] ? TupleToUnion<Q, R | P> : R
description

R | P 是作为联合类型返回的,通过递归进行联合

description

T extends [infer P, ...infer Q] 而不是反过来的

Type parameters

  • T: any[]

  • R = T[0]

TupleToUnion1<T>: T extends [...infer R] ? R[number] : never
description

元组/数组 直接通过[number]访问,可以得到内部的类型

Type parameters

  • T: any[]

TypeTransferValues<T>: [T] extends [object] ? { readonly [ P in keyof T]: T[P] }[keyof T] : T

接受一个object type,返回一个values 如果不是object,则返回原值

Type parameters

  • T

UnionToIntersection<T>: (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never
name

UnionToIntersection

description

请实现UnionToIntersection用以从Union得到Intersection。

example

type A = UnionToIntersection<{a: string} | {b: string} | {c: string}> // {a: string} & {b: string} & {c: string}

url

https://fettblog.eu/typescript-union-to-intersection/

不知道什么是逆变位置,为什么放到条件类型中,TS就会有装包和解包的操作

Type parameters

  • T

UnwrapPromise<T>: T extends Promise<infer P> ? P : never
name

UnwrapPromise

description

实现UnwrapPromise以返回已解决的承诺类型。

example

type A = UnwrapPromise<Promise> // string

example

type B = UnwrapPromise<Promise> // null

example

type C = UnwrapPromise // Error

Type parameters

  • T: Promise<any>

Generated using TypeDoc