Master TypeScript's advanced type system including generics, conditional types, mapped types, template literals, and utility types for building type-safe applications. Use when implementing complex type logic, creating reusable type utilities, or ensuring compile-time type safety in TypeScript projects.
Standardmäßig ist der Prompt ausgewählt, der zuerst die Quelle prüft. Sie können zu einem direkten Befehl wechseln oder eine lokale Kopie herunterladen.
Quelldateien prüfen
Lesen Sie SKILL.md und alle von SkillsMP angezeigten Begleitdateien, bevor Sie sich für eine Installation entscheiden.
Mit Codex oder Claude installieren Kopieren Sie diesen Prompt, fügen Sie ihn in Codex, Claude oder einen anderen Assistant ein und lassen Sie die Skill-Seite prüfen und installieren.
Ein direkter Befehl überspringt den Prüf-Prompt. Prüfen Sie die Quelle, bevor Sie ihn ausführen.
Master TypeScript's advanced type system including generics, conditional types, mapped types, template literals, and utility types for building type-safe applications. Use when implementing complex type logic, creating reusable type utilities, or ensuring compile-time type safety in TypeScript projects.
TypeScript Advanced Types
Comprehensive guidance for mastering TypeScript's advanced type system including generics, conditional types, mapped types, template literal types, and utility types for building robust, type-safe applications.
When to Use This Skill
Building type-safe libraries or frameworks
Creating reusable generic components
Implementing complex type inference logic
Designing type-safe API clients
Building form validation systems
Creating strongly-typed configuration objects
Implementing type-safe state management
Migrating JavaScript codebases to TypeScript
Core Concepts
1. Generics
Purpose: Create reusable, type-flexible components while maintaining type safety.
Basic Generic Function:
function identity<T>(value: T): T {
return value;
}
const num = identity<number>(42); // Type: numberconst str = identity<string>('hello'); // Type: stringconst auto = identity(true);
// Type inferred: boolean
Generic Constraints:
interfaceHasLength {
length: number;
}
function logLength<T extendsHasLength>(item: T): T {
console.log(item.length);
return item;
}
logLength('hello'); // OK: string has lengthlogLength([1, 2, 3]); // OK: array has lengthlogLength({ length: 10 }); // OK: object has length// logLength(42); // Error: number has no length
Multiple Type Parameters:
function merge<T, U>(obj1: T, obj2: U): T & U {
return { ...obj1, ...obj2 };
}
const merged = merge({ name: 'John' }, { age: 30 });
// Type: { name: string } & { age: number }
2. Conditional Types
Purpose: Create types that depend on conditions, enabling sophisticated type logic.
Basic Conditional Type:
typeIsString<T> = T extendsstring ? true : false;
type A = IsString<string>; // truetype B = IsString<number>; // false
// Extract array element typetypeElementType<T> = T extends (infer U)[] ? U : never;
typeNumArray = number[];
typeNum = ElementType<NumArray>; // number// Extract promise typetypePromiseType<T> = T extendsPromise<infer U> ? U : never;
typeAsyncNum = PromiseType<Promise<number>>; // number// Extract function parameterstypeParameters<T> = T extends (...args: infer P) => any ? P : never;
functionfoo(a: string, b: number) {}
typeFooParams = Parameters<typeof foo>; // [string, number]
2. Type Guards
functionisString(value: unknown): value is string {
returntypeof value === 'string';
}
function isArrayOf<T>(value: unknown, guard: (item: unknown) => item is T): value is T[] {
returnArray.isArray(value) && value.every(guard);
}
constdata: unknown = ['a', 'b', 'c'];
if (isArrayOf(data, isString)) {
data.forEach(s => s.toUpperCase()); // Type: string[]
}
3. Assertion Functions
functionassertIsString(value: unknown): asserts value is string {
if (typeof value !== 'string') {
thrownewError('Not a string');
}
}
functionprocessValue(value: unknown) {
assertIsString(value);
// value is now typed as stringconsole.log(value.toUpperCase());
}
Best Practices
Use unknown over any: Enforce type checking
Prefer interface for object shapes: Better error messages
Use type for unions and complex types: More flexible
Leverage type inference: Let TypeScript infer when possible
Create helper types: Build reusable type utilities
Use const assertions: Preserve literal types
Avoid type assertions: Use type guards instead
Document complex types: Add JSDoc comments
Use strict mode: Enable all strict compiler options
Test your types: Use type tests to verify type behavior