TypeScript高级特性提升代码质量TypeScript是JavaScript的超集提供了强大的类型系统。掌握高级特性能够写出更安全、更可维护的代码。泛型编程基本泛型function identityT(arg: T): T { return arg } const num identitynumber(42) const str identitystring(hello)泛型约束interface Lengthwise { length: number } function logLengthT extends Lengthwise(arg: T): T { console.log(arg.length) return arg }泛型类class ContainerT { private items: T[] [] add(item: T) { this.items.push(item) } get(index: number): T | undefined { return this.items[index] } }类型体操条件类型type IsStringT T extends string ? true : false type A IsStringstring // true type B IsStringnumber // false映射类型type ReadonlyT { readonly [P in keyof T]: T[P] } type PartialT { [P in keyof T]?: T[P] }模板字面量类型type Color red | green | blue type Size small | medium | large type ColorSize ${Color}-${Size} // red-small | red-medium | red-large | ...高级类型技巧infer关键字type ReturnTypeT T extends (...args: any[]) infer R ? R : never type Fn () string type Result ReturnTypeFn // string条件类型嵌套type DeepReadonlyT { readonly [P in keyof T]: T[P] extends object ? DeepReadonlyT[P] : T[P] }装饰器类装饰器function sealed(constructor: Function) { Object.seal(constructor) Object.seal(constructor.prototype) } sealed class Greeter { greeting: string constructor(message: string) { this.greeting message } }属性装饰器function format(target: any, propertyKey: string) { let value target[propertyKey] const getter () value const setter (newValue: string) { value newValue.toUpperCase() } Object.defineProperty(target, propertyKey, { get: getter, set: setter, enumerable: true, configurable: true }) } class User { format name: string }模块系统ES模块// utils.ts export function add(a: number, b: number): number { return a b } // app.ts import { add } from ./utils命名空间namespace Validation { export function validateEmail(email: string): boolean { return /^[^\s][^\s]\.[^\s]$/.test(email) } } Validation.validateEmail(testexample.com)类型声明声明文件// module.d.ts declare module some-module { export function doSomething(): void }合并声明interface User { name: string } interface User { age: number } // User now has both name and age编译器配置tsconfig.json{ compilerOptions: { target: ES2020, module: ESNext, strict: true, esModuleInterop: true, skipLibCheck: true, forceConsistentCasingInFileNames: true } }总结TypeScript的高级特性能够帮助我们写出更安全、更可维护的代码。掌握这些特性能够提升我们的开发效率和代码质量。技术有温度TypeScript让代码更加健壮和可靠。
TypeScript高级特性:提升代码质量
发布时间:2026/5/30 18:15:42
TypeScript高级特性提升代码质量TypeScript是JavaScript的超集提供了强大的类型系统。掌握高级特性能够写出更安全、更可维护的代码。泛型编程基本泛型function identityT(arg: T): T { return arg } const num identitynumber(42) const str identitystring(hello)泛型约束interface Lengthwise { length: number } function logLengthT extends Lengthwise(arg: T): T { console.log(arg.length) return arg }泛型类class ContainerT { private items: T[] [] add(item: T) { this.items.push(item) } get(index: number): T | undefined { return this.items[index] } }类型体操条件类型type IsStringT T extends string ? true : false type A IsStringstring // true type B IsStringnumber // false映射类型type ReadonlyT { readonly [P in keyof T]: T[P] } type PartialT { [P in keyof T]?: T[P] }模板字面量类型type Color red | green | blue type Size small | medium | large type ColorSize ${Color}-${Size} // red-small | red-medium | red-large | ...高级类型技巧infer关键字type ReturnTypeT T extends (...args: any[]) infer R ? R : never type Fn () string type Result ReturnTypeFn // string条件类型嵌套type DeepReadonlyT { readonly [P in keyof T]: T[P] extends object ? DeepReadonlyT[P] : T[P] }装饰器类装饰器function sealed(constructor: Function) { Object.seal(constructor) Object.seal(constructor.prototype) } sealed class Greeter { greeting: string constructor(message: string) { this.greeting message } }属性装饰器function format(target: any, propertyKey: string) { let value target[propertyKey] const getter () value const setter (newValue: string) { value newValue.toUpperCase() } Object.defineProperty(target, propertyKey, { get: getter, set: setter, enumerable: true, configurable: true }) } class User { format name: string }模块系统ES模块// utils.ts export function add(a: number, b: number): number { return a b } // app.ts import { add } from ./utils命名空间namespace Validation { export function validateEmail(email: string): boolean { return /^[^\s][^\s]\.[^\s]$/.test(email) } } Validation.validateEmail(testexample.com)类型声明声明文件// module.d.ts declare module some-module { export function doSomething(): void }合并声明interface User { name: string } interface User { age: number } // User now has both name and age编译器配置tsconfig.json{ compilerOptions: { target: ES2020, module: ESNext, strict: true, esModuleInterop: true, skipLibCheck: true, forceConsistentCasingInFileNames: true } }总结TypeScript的高级特性能够帮助我们写出更安全、更可维护的代码。掌握这些特性能够提升我们的开发效率和代码质量。技术有温度TypeScript让代码更加健壮和可靠。