Advanced TypeScript
Advanced TypeScript
Phần tiêu đề “Advanced TypeScript”Các topic nâng cao cho TypeScript power users.
Mapped Types
Phần tiêu đề “Mapped Types”type Readonly<T> = { readonly [P in keyof T]: T[P];};
type Optional<T> = { [P in keyof T]?: T[P];};Conditional Types
Phần tiêu đề “Conditional Types”type IsString<T> = T extends string ? true : false;
type A = IsString<string>; // truetype B = IsString<number>; // falseTemplate Literal Types
Phần tiêu đề “Template Literal Types”type EmailLocale = "en" | "vi";type EmailType = "welcome" | "reset";
type EmailTemplate = `${EmailLocale}_${EmailType}`;// "en_welcome" | "en_reset" | "vi_welcome" | "vi_reset"Decorators (Experimental)
Phần tiêu đề “Decorators (Experimental)”function log(target: any, key: string) { console.log(`${key} was called`);}
class MyClass { @log myMethod() { // ... }}Best Practices
Phần tiêu đề “Best Practices”- Prefer interfaces over types cho object shapes
- Use strict mode trong tsconfig.json
- Avoid
any- dùngunknownnếu cần - Enable strictNullChecks
- Document complex types