Created
December 10, 2021 19:13
-
-
Save ali-master/07610e3ea5c64f54ca9feebb229a4ba8 to your computer and use it in GitHub Desktop.
Typescript Workshop(First session)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////// | |
// Exclude | |
//////////////////// | |
type MyExclude<T, L> = T extends L ? never : T; | |
type TestExclude = MyExclude<"a" | "b" | "c" | "d", "a" | "b"> | |
//////////////////// | |
// Extract | |
//////////////////// | |
type MyExtract<T, L> = T extends L ? T : never; | |
type TestExtract = MyExtract<"a" | "b" | "c" | "d", "a" | "b"> | |
//////////////////// | |
// Readonly | |
//////////////////// | |
type MyReadonly<T> = { | |
readonly [P in keyof T]: T[P]; | |
}; | |
type NonReadonlyType = { | |
a: string; | |
b: string; | |
c: number; | |
} | |
type TestMyReadonly = MyReadonly<NonReadonlyType> | |
//////////////////// | |
// Optional | |
//////////////////// | |
type MyOptional<T> = { | |
[P in keyof T]?: T[P]; | |
}; | |
type RequiredType = { | |
a: string; | |
b: string; | |
c: number; | |
} | |
type TestMyOptional = MyOptional<RequiredType> | |
//////////////////// | |
// Required | |
//////////////////// | |
type MyRequired<T> = { | |
[P in keyof T]-?: T[P]; | |
}; | |
type NonRequiredType = { | |
a?: string; | |
b?: string; | |
c: number; | |
} | |
type TestMyRequired = MyRequired<NonRequiredType> | |
//////////////////// | |
// Record | |
//////////////////// | |
type MyRecord<Keys extends keyof any, Value> = { | |
[Prop in Keys]: Value; | |
}; | |
type TestMyRecord = MyRecord<"name" | "family", string> | |
//////////////////// | |
// Pick | |
//////////////////// | |
type MyPick<T, K extends keyof T> = { | |
[P in K]: T[P] | |
} | |
type TestPick = MyPick<{a: string, b: string, c: number}, "a">; | |
//////////////////// | |
// Omit | |
//////////////////// | |
type MyOmit<T, K extends keyof any> = MyPick<T, MyExclude<keyof T, K>> | |
type TestOmit = MyOmit<{a: string, b: string, c: number}, "a"> | |
// Another Solution | |
type MyOmit2<T, Keys extends keyof any> = { | |
[P in keyof T as P extends Keys ? never : P]: T[P]; | |
} | |
//////////////////// | |
// If | |
//////////////////// | |
type MyIf<B, P1, P2> = B extends true ? P1 : P2; | |
type TestIfA = MyIf<true, "a", "b"> // a | |
type TestIB = MyIf<false, "a", "b"> // b | |
//////////////////// | |
// Unshift | |
//////////////////// | |
type Unshift<T extends any[], V = any> = [V, ...T]; | |
type Result = Unshift<[1, 2], 0> // [0, 1, 2,] | |
//////////////////// | |
// Push | |
//////////////////// | |
type Push<T extends any[], V = any> = [...T, V]; | |
type ResultPush = Push<[1, 2], "3"> // [1, 2, '3'] | |
//////////////////// | |
// Concat | |
//////////////////// | |
type Concat<T extends any[], K extends any[]> = [...T, ...K]; | |
type ResultConcat = Concat<[1], [2]> // expected to be [1, 2] | |
//////////////////// | |
// Merge | |
//////////////////// | |
type Merge<T, U> = MyOmit<T, keyof U> & U; | |
type ResultMerge = Merge<{name: string; age: number}, {family: string; name: string}> // expected to be {name: string; age: number; family: string} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment