当前位置: 首页 > news >正文

typescript中的难点总结

TypeScript 虽然在 JavaScript 基础上增加了类型系统,但在实际使用中仍有一些复杂和容易混淆的概念。以下是 TypeScript 中的主要难点总结:

1. 复杂类型系统

泛型约束与条件类型

// 泛型约束
interface Lengthwise {length: number;
}function logLength<T extends Lengthwise>(arg: T): T {console.log(arg.length);return arg;
}// 条件类型
type IsString<T> = T extends string ? true : false;
type A = IsString<'hello'>; // true
type B = IsString<123>; // false// 映射类型中的条件判断
type ExtractPropertyNames<T, U> = {[K in keyof T]: T[K] extends U ? K : never;
}[keyof T];

类型推断与类型收窄

// 类型保护
function isString(value: unknown): value is string {return typeof value === 'string';
}function processValue(value: string | number) {if (isString(value)) {// 这里 value 被收窄为 stringconsole.log(value.toUpperCase());} else {// 这里 value 被收窄为 numberconsole.log(value.toFixed(2));}
}

2. 高级类型技巧

模板字面量类型

type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE';
type ApiEndpoint = `/${string}`;
type FullEndpoint = `${HttpMethod} ${ApiEndpoint}`;type UserAction = 'create' | 'update' | 'delete';
type EventName = `${UserAction}_user`;// 字符串操作类型
type Getter<T extends string> = `get${Capitalize<T>}`;
type NameGetters = Getter<'name' | 'age'>; // "getName" | "getAge"

递归类型

// 递归类型定义
type Json = | string| number| boolean| null| { [key: string]: Json }| Json[];// 递归工具类型
type DeepReadonly<T> = {readonly [P in keyof T]: T[P] extends object ? DeepReadonly<T[P]>: T[P];
}type DeepPartial<T> = {[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]>: T[P];
}

3. 声明合并

// 接口合并
interface User {name: string;
}interface User {age: number;
}// 最终 User 接口为:
// interface User {
//   name: string;
//   age: number;
// }// 命名空间合并
namespace Validation {export interface StringValidator {isAcceptable(s: string): boolean;}
}namespace Validation {export class LettersOnlyValidator implements StringValidator {isAcceptable(s: string) {return /^[A-Za-z]+$/.test(s);}}
}

4. 装饰器元编程

// 类装饰器
function Logger<T extends { new (...args: any[]): {} }>(constructor: T) {return class extends constructor {constructor(...args: any[]) {super(...args);console.log(`Created instance of ${constructor.name}`);}};
}// 方法装饰器
function LogMethod(target: any, propertyName: string, descriptor: PropertyDescriptor) {const originalMethod = descriptor.value;descriptor.value = function (...args: any[]) {console.log(`Calling ${propertyName} with args:`, args);return originalMethod.apply(this, args);};return descriptor;
}@Logger
class Calculator {@LogMethodadd(a: number, b: number): number {return a + b;}
}

5. 模块和类型解析

类型引用和路径映射

// tsconfig.json 中的路径映射
{"compilerOptions": {"baseUrl": ".","paths": {"@/*": ["src/*"],"@/components/*": ["src/components/*"]}}
}// 三斜线指令
/// <reference types="node" />
/// <reference path="./types.d.ts" />

6. 泛型进阶用法

泛型约束与默认值

// 复杂的泛型约束
interface Entity {id: number;
}interface User extends Entity {name: string;
}interface Product extends Entity {title: string;price: number;
}// 泛型工厂函数
function createRepository<T extends Entity, K extends keyof T>() {return class Repository {private items: T[] = [];findById(id: number): T | undefined {return this.items.find(item => item.id === id);}findByField(field: K, value: T[K]): T | undefined {return this.items.find(item => item[field] === value);}};
}const UserRepository = createRepository<User, keyof User>();

逆变、协变和双向协变

// 协变示例
interface Animal {name: string;
}interface Dog extends Animal {breed: string;
}let animals: Animal[] = [];
let dogs: Dog[] = [];animals = dogs; // 协变 - 允许
// dogs = animals; // 错误 - 不允许// 逆变示例(函数参数)
type AnimalHandler = (animal: Animal) => void;
type DogHandler = (dog: Dog) => void;let animalHandler: AnimalHandler = (animal: Animal) => { };
let dogHandler: DogHandler = (dog: Dog) => { };// dogHandler = animalHandler; // 协变 - 允许(strictFunctionTypes 下不允许)
// animalHandler = dogHandler; // 逆变 - 允许

7. 工具类型深度使用

// 内置工具类型进阶用法
type RequiredByKeys<T, K extends keyof T = keyof T> = Omit<T, K> & Required<Pick<T, K>>;type PartialByKeys<T, K extends keyof T = keyof T> = Omit<T, K> & Partial<Pick<T, K>>;// 条件类型与 infer 关键字
type MyReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
type MyParameters<T> = T extends (...args: infer P) => any ? P : never;// 递归工具类型
type Nullable<T> = {[P in keyof T]: T[P] | null;
};type DeepNullable<T> = {[P in keyof T]: T[P] extends object ? DeepNullable<T[P]> | null: T[P] | null;
};

8. 命名空间和模块的混合使用

// 命名空间中的模块导出
namespace Shapes {export interface Point {x: number;y: number;}export class Rectangle {constructor(public width: number, public height: number) {}}
}// 模块中的命名空间导出
export module Geometry {export interface Vector {x: number;y: number;z: number;}export class Matrix {// 矩阵实现}
}

9. 配置复杂性

tsconfig.json 复杂配置

{"compilerOptions": {"strict": true,"noImplicitAny": true,"strictNullChecks": true,"strictFunctionTypes": true,"strictBindCallApply": true,"strictPropertyInitialization": true,"noImplicitThis": true,"noImplicitReturns": true,"exactOptionalPropertyTypes": true,"noUncheckedIndexedAccess": true,"noImplicitOverride": true}
}

10. 第三方库类型定义

声明文件编写

// 为第三方库编写类型声明
declare module 'my-untyped-library' {interface LibraryOptions {timeout?: number;retries?: number;}export function initialize(options: LibraryOptions): void;export function doSomething(input: string): Promise<string>;
}// 全局类型扩展
declare global {interface Window {myCustomProperty: string;}namespace NodeJS {interface ProcessEnv {NODE_ENV: 'development' | 'production' | 'test';API_URL: string;}}
}

这些难点需要在实际开发中不断实践和总结,才能更好地掌握 TypeScript 的强大功能。

http://www.dtcms.com/a/474309.html

相关文章:

  • PHP 字符串处理详解
  • 【JUC】线程池有哪些拒绝策略?该如何选择使用?
  • 4 随机数 从一个随机数到另外一个随机数、等概率随机
  • 机器学习17:如何有效使用自监督式学习
  • 生成对抗网络(GAN)及其变种:CycleGAN和StarGAN
  • dede网站地图html文件公司部门撤销要求转岗不同意怎么办
  • 国外购买空间的网站有哪些最优惠的网站优化
  • Linux安装JDK1.8 tomcat MariaDB(MySQL删减版)
  • 【C++】C++中的异常处理try-catch
  • 珠海专业做网站的公司交友软件
  • rclone:安装与配置
  • 第128题 最长连续序列
  • 深度学习》【项目】自然语言处理——情感分析 <上>
  • 在哪里申请网站域名免费制作表白网页
  • 外设模块学习(6)——DHT11温湿度传感器(STM32)
  • 创造网站软件icp备案查询
  • 计算机视觉——从YOLO系列演进到YOLOv12架构创新、注意力机制优化、推理实践与性能基准
  • 广州网站建设公司广州企业网站建设公司公司网站建设网站建设合同需要印花税
  • 门户网站开发需求律师网络推广
  • FSR《软件开发可行性分析报告》全面概述
  • 鸿蒙开发实战:从零构建可扩展的数据迁移机制,让数据库升级不再崩
  • java接收小程序发送的protobuf消息
  • 沧州市高速公路建设管理局网站龙岩天宫山有几个台阶
  • 闽侯做网站做国际物流需要哪些网站
  • 【Swift】LeetCode 49. 字母异位词分组
  • 对网站建设建议外加工活怎么直接找厂家接单
  • (17)100天python从入门到拿捏《正则表达式》
  • 【C++】深入理解list底层:list的模拟实现
  • 用Spring Cloud打造健壮服务:熔断与降级如何护航核心业务
  • 网站平台怎么推广企业的做网站