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
| function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] { | |
| return obj[key]; | |
| } | |
| const user = { id: 42, name: "Alice" }; | |
| // Infers return type as number | |
| const userId = getProperty(user, "id"); | |
| // Infers return type as string |
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
| function updateProperty<T, K extends keyof T>( | |
| obj: T, | |
| key: K, | |
| value: T[K] | |
| ): T { | |
| return { ...obj, [key]: value }; | |
| } | |
| interface Config { | |
| port: number; |
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
| type PathsToStringProps<T> = { | |
| [K in keyof T]: T[K] extends string | |
| ? K | |
| : T[K] extends object | |
| ? `${K & string}.${PathsToStringProps<T[K]> & string}` | |
| : never; | |
| }[keyof T]; | |
| function getNestedString<T, P extends PathsToStringProps<T>>( | |
| obj: T, |
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
| type IsString<T> = T extends string ? true : false; | |
| type A = IsString<"hello">; // true | |
| type B = IsString<42>; // false | |
| type ExtractStrings<T> = T extends string ? T : never; | |
| type C = ExtractStrings<"a" | "b" | 42 | true>; // "a" | "b" |
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
| type Nullable<T> = { | |
| [K in keyof T]: T[K] | null; | |
| }; | |
| type Optional<T> = { | |
| [K in keyof T]?: T[K]; | |
| }; | |
| type DeepReadonly<T> = { | |
| readonly [K in keyof T]: T[K] extends object ? DeepReadonly<T[K]> : T[K]; |
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
| type ReturnType<T> = T extends (...args: any[]) => infer R ? R : never; | |
| function getUser(): { id: number; name: string } { | |
| return { id: 1, name: "Alice" }; | |
| } | |
| type User = ReturnType<typeof getUser>; | |
| // { id: number; name: string } | |
| type FirstArg<T> = T extends (arg: infer A, ...rest: any[]) => any ? A : never; |
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
| type RequireKeys<T, K extends keyof T> = T & Required<Pick<T, K>>; | |
| interface Config { | |
| host?: string; | |
| port?: number; | |
| debug?: boolean; | |
| } | |
| type ProductionConfig = RequireKeys<Config, "host" | "port">; | |
| // { host: string; port: number; debug?: boolean } |
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
| type RequiredKeys<T> = { | |
| [K in keyof T]-?: {} extends Pick<T, K> ? never : K; | |
| }[keyof T]; | |
| function getRequiredProperty<T, K extends RequiredKeys<T>>( | |
| obj: T, | |
| key: K | |
| ): T[K] { | |
| return obj[key]; // Guaranteed to be defined | |
| } |
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
| // Overconstrained: requires full User type | |
| function logUserId<T extends User>(user: T): void { | |
| console.log(user.id); | |
| } | |
| // Correctly constrained: requires only id property | |
| function logId<T extends { id: number }>(obj: T): void { | |
| console.log(obj.id); | |
| } |
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
| interface DatabaseConfig { | |
| host: string; | |
| port: number; | |
| ssl: boolean; | |
| poolSize: number; | |
| } | |
| const defaults: Partial<DatabaseConfig> = { | |
| ssl: true, | |
| poolSize: 10, |