Type Challenge
Easy type challenges
Updated date: 2024-11-21
Type Challenge
Solutons
- Hello World
- answer
type HelloWorld = string;
- answer
- Pick
- answer
type MyPick<T, K extends keyof T> = { [P in K]: T[P]; };
- tips
- "|" represents union type
- answer
- Readonly
- answer
type MyReadonly<T> = { readonly [P in keyof T]: T[P]; };
- tips
- "readonly" represents Readonly modifier
- answer
- Tuple to Object
- answer
type TupleToObject<T extends readonly PropertyKey[]> = { [key in T[number]]: key; };
- tips
- See Mapped Types
- answer
- First of Array
- answer
type First<T extends any[]> = T extends [] ? never : T[0];
- tips
- "never" represents never type
T extends []
is a specific check to determine if T is an empty array. It evaluates to true only if T is an empty array, allowing the type to distinguish between empty and non-empty arrays.
- answer
- Length of Tuple
- answer
type Length<T> = T extends readonly any[] ? T["length"] : Length<T>;
- tips:
- Exclude
- answer
type MyExclude<T, U> = T extends U ? never : T;
- answer
- Awaited
- answer
type MyAwaited<T> = T extends Promise<infer K> ? MyAwaited<K> : T;
- tips:
- answer
- If
- answer
type If<C extends boolean, T, F> = C extends true ? T : F;
- answer
- Concat
- answer
type Concat<T extends any[], U extends any[]> = [...T, ...U];
- answer
- Includes
- answer
type Includes<T extends readonly any[], U> = U extends T[number] ? true : false;
- answer
- Push
- answer
type Push<T extends any[], U> = [...T, U];
- answer
- Unshift
- answer
type Unshift<T extends any[], U> = [U, ...T];
- answer
- Parameters
- answer
type MyParameters<T extends (...args: any) => any> = T extends ( ...args: infer P ) => any ? P : never;
- answer