Typescript Advanced

Generic Type

function identity<T>(value: T): T {
    return value;
  }
  
  const result = identity<number>(42);
  
  // Output:
  // 42
  // ...
  // ...
Explore Generic Type

Implement Utility Type

type MyPartial<T> = {
    [K in keyof T]?: T[K];
  };
  
  type User = { id: number; name: string };
  type PartialUser = MyPartial<User>;
  
  // Output: id?: number; name?: string
Explore Utility Types

Mapped Type

type MappedType = 'primary' | 'secondary' | 'default'

type ObjMappedType = {
  [Type in MappedType]: Type
}

// Output:
// type ObjMappedType = {
//    primary: "primary";
//    secondary: "secondary";
//    default: "default";
// }

type Type = {
  name: string;
  surname: string;
  age: number,
  isManager: boolean
}

type MappedType = {
  [Type in keyof Type]?: Type[Type] | null
}

// Output:
// type MappedKeyofType = {
//     name?: string | null | undefined;
//     surname?: string | null | undefined;
//     age?: number | null | undefined;
//     isManager?: boolean | null | undefined;
// }
Explore Mapped Type

String Type

type Greeting = `Hello ${string}`;
  
  const greet: Greeting = "Hello Ali";
  
  // Output:
  // greet: "Hello Ali"
  // ...
  // ...
Explore String Types

Conditional Type

type ConditionalType<T> = T extends string ? T : never
type test = ConditionalType<'test1' | 12 | false>

// Output: 
// test: 'test1' 
// ...
// ...
Explore Conditional Type

Map Access

type KeyOfType = {
  name: string;
  surname: string;
  age: number,
  isManager: boolean
}

type KeyofTest = keyof KeyOfType
const testKeyofTest: KeyofTest = 'age'

// Map Access
type MapAccess = KeyOfType['name'] | KeyOfType['age']
// output:
// string | number

type MapAccess2 = KeyOfType[keyof KeyOfType]
// output:
// string | number | boolean

// ...
//...
Map Access Detail

Infer

type InferArrayType<T> = T extends (infer R)[] ? R : T

type Test1 = InferArrayType<string[]> // string
type Test2 = InferArrayType<number[]> // number
Explore Infer