Typescript İleri Düzey

Generic Type

function identity<T>(value: T): T {
    return value;
  }
  
  const result = identity<number>(42);
  
  // Output:
  // 42
  // ...
  // ...
Generic Tipleri Keşfet

Yardımcı Tip Uygulamaları

type MyPartial<T> = {
    [K in keyof T]?: T[K];
  };
  
  type User = { id: number; name: string };
  type PartialUser = MyPartial<User>;
  
  // Output: id?: number; name?: string
Yardımcı Tipleri Keşfet

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;
// }
Mapped Tipleri Keşfet

String Type

type Greeting = `Hello ${string}`;
  
  const greet: Greeting = "Hello Ali";
  
  // Output:
  // greet: "Hello Ali"
  // ...
  // ...
String Tipleri Keşfet

Conditional(şartlı) Type

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

// Output: 
// test: 'test1' 
// ...
// ...
Şartlı Tipleri Keşfet

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 Keşfet

Infer Type

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

type Test1 = InferArrayType<string[]> // string
type Test2 = InferArrayType<number[]> // number
Infer Type Keşfet