Easy
- MyPick
ts
type MyPick<T, K extends keyof T> = { [P in K]: T[P] }
- MyReadonly
ts
type MyReadonly<T> = { readonly [P in keyof T]: T[P] }
- TupleToObject
ts
type TupleToObject<T extends readonly PropertyKey[]> = { [P in T[number]]: P }
T[number]
:indexed access types
4. First
ts
type First<T extends any[]> = T extends [] ? never : T[0]
- Length
ts
type Length<T extends readonly any[]> = T['length']
- MyExclude
ts
type MyExclude<T, K> = T extends K ? never : T
- MyAwaited
ts
type MyAwaited<T> = T extends Promise<infer one> ? one : never
- ExampleType
ts
type ExampleType = Promise<string>
- If
ts
type If<C extends boolean, T, F> = C extends true ? T : F
- Concat
ts
type Concat<T extends any[], K extends any[]> = [...T, ...K]
- Includes
ts
type Includes<T extends readonly unknown[], U> = U extends T[number] ? true : false
- Push
ts
type Push<T extends any[], P> = [...T, P]
- Unshift
ts
type Unshift<T extends any[], P> = [P, ...T]
Medium
- MyReturnType
ts
type MyReturnType<T> = T extends (...args:any[])=>infer R?R:never
- My Omit
ts
type MyExclude<T,K> = T extends K?never:T
type MyOmit<T,P extends keyof T> = {
[K in MyExclude<keyof T,P>]:T[K]
}
- MyReadonly2
ts
type MyReadonly2<T,K extends keyof T = keyof T> = {
readonly [P in K]:T[P]
}&{ [S in MyExclude<keyof T,K>] :T[S] }