Skip to content

Instantly share code, notes, and snippets.

View pfftdammitchris's full-sized avatar
💭
Dreaming

Christopher Tran pfftdammitchris

💭
Dreaming
View GitHub Profile
@pfftdammitchris
pfftdammitchris / snippet-2.ts
Created July 18, 2026 06:06
TypeScript Generic Constraints in Depth: `extends`, `keyof`, and the Patterns That Prevent Runtime Errors - snippet-2.ts
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
@pfftdammitchris
pfftdammitchris / snippet-3.ts
Created July 18, 2026 06:06
TypeScript Generic Constraints in Depth: `extends`, `keyof`, and the Patterns That Prevent Runtime Errors - snippet-3.ts
function updateProperty<T, K extends keyof T>(
obj: T,
key: K,
value: T[K]
): T {
return { ...obj, [key]: value };
}
interface Config {
port: number;
@pfftdammitchris
pfftdammitchris / snippet-4.ts
Created July 18, 2026 06:06
TypeScript Generic Constraints in Depth: `extends`, `keyof`, and the Patterns That Prevent Runtime Errors - snippet-4.ts
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,
@pfftdammitchris
pfftdammitchris / snippet-5.ts
Created July 18, 2026 06:06
TypeScript Generic Constraints in Depth: `extends`, `keyof`, and the Patterns That Prevent Runtime Errors - snippet-5.ts
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"
@pfftdammitchris
pfftdammitchris / snippet-6.ts
Created July 18, 2026 06:06
TypeScript Generic Constraints in Depth: `extends`, `keyof`, and the Patterns That Prevent Runtime Errors - snippet-6.ts
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];
@pfftdammitchris
pfftdammitchris / snippet-7.ts
Created July 18, 2026 06:06
TypeScript Generic Constraints in Depth: `extends`, `keyof`, and the Patterns That Prevent Runtime Errors - snippet-7.ts
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;
@pfftdammitchris
pfftdammitchris / snippet-8.ts
Created July 18, 2026 06:06
TypeScript Generic Constraints in Depth: `extends`, `keyof`, and the Patterns That Prevent Runtime Errors - snippet-8.ts
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 }
@pfftdammitchris
pfftdammitchris / snippet-9.ts
Created July 18, 2026 06:06
TypeScript Generic Constraints in Depth: `extends`, `keyof`, and the Patterns That Prevent Runtime Errors - snippet-9.ts
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
}
@pfftdammitchris
pfftdammitchris / snippet-10.ts
Created July 18, 2026 06:06
TypeScript Generic Constraints in Depth: `extends`, `keyof`, and the Patterns That Prevent Runtime Errors - snippet-10.ts
// 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);
}
@pfftdammitchris
pfftdammitchris / snippet-11.ts
Created July 18, 2026 06:06
TypeScript Generic Constraints in Depth: `extends`, `keyof`, and the Patterns That Prevent Runtime Errors - snippet-11.ts
interface DatabaseConfig {
host: string;
port: number;
ssl: boolean;
poolSize: number;
}
const defaults: Partial<DatabaseConfig> = {
ssl: true,
poolSize: 10,