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

【TS学习】(20)有关this的类型处理

在 TypeScript 中,this 类型体操(Type Gymnastics with this 是一种高级用法,通常用于实现复杂的类型推断和链式调用。通过结合 this 类型、条件类型、泛型和其他高级特性,可以实现非常灵活的类型操作。


1. 基础:this 类型

(1) 定义
  • 在 TypeScript 中,this 类型表示当前类的实例。
  • 它允许方法返回当前实例本身,从而支持链式调用。
示例
class Counter {
  private count: number = 0;

  increment(): this {
    this.count++;
    return this;
  }

  decrement(): this {
    this.count--;
    return this;
  }

  getCount(): number {
    return this.count;
  }
}

const counter = new Counter();
console.log(counter.increment().decrement().getCount()); // 输出 0

在这里:

  • incrementdecrement 方法返回 this,使得可以连续调用这些方法。

2. 高级:this 类型与继承

在继承中,this 类型会自动推断为子类的类型。这种行为被称为多态的 this 类型

示例
class Animal {
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  setName(name: string): this {
    this.name = name;
    return this;
  }
}

class Dog extends Animal {
  bark(): this {
    console.log(`${this.name} barks`);
    return this;
  }
}

const dog = new Dog("Rex");
dog.setName("Max").bark(); // 输出 "Max barks"

在这里:

  • setName 方法返回 this,因此可以在链式调用中无缝地调用子类的 bark 方法。

3. 条件类型与 this

通过结合条件类型和 this 类型,可以实现动态的类型推断。

示例:动态方法返回类型
class Builder<T> {
  private value: T | null = null;

  setValue(value: T): this {
    this.value = value;
    return this;
  }

  getValue(): T {
    if (this.value === null) {
      throw new Error("Value is not set");
    }
    return this.value;
  }

  reset(): this {
    this.value = null;
    return this;
  }

  transform<U>(transformFn: (value: T) => U): Builder<U> {
    const newValue = transformFn(this.getValue());
    return new Builder<U>().setValue(newValue);
  }
}

const builder = new Builder<number>()
  .setValue(42)
  .transform((value) => value.toString())
  .transform((value) => value.toUpperCase());

console.log(builder.getValue()); // 输出 "42"

在这里:

  • transform 方法使用条件类型和泛型动态地改变了 Builder 的类型。

4. 泛型与 this 的结合

通过将泛型与 this 类型结合,可以实现更加灵活的设计。

示例:类型安全的链式调用
class Chainable<T> {
  private value: T;

  constructor(value: T) {
    this.value = value;
  }

  getValue(): T {
    return this.value;
  }

  map<U>(fn: (value: T) => U): Chainable<U> {
    return new Chainable(fn(this.value));
  }

  mutate(fn: (value: T) => void): this {
    fn(this.value);
    return this;
  }
}

const chain = new Chainable(10)
  .mutate((value) => console.log(`Current value: ${value}`))
  .map((value) => value * 2)
  .map((value) => value.toString());

console.log(chain.getValue()); // 输出 "20"

在这里:

  • mutate 方法返回 this,支持链式调用。
  • map 方法使用泛型动态地改变了 Chainable 的类型。

5. 条件推断与 this

通过结合条件推断和 this 类型,可以实现更复杂的类型操作。

示例:动态扩展方法
type MethodMap<T> = {
  [K in keyof T]: (this: T, ...args: any[]) => T;
};

class Dynamic<T extends MethodMap<T>> {
  call<K extends keyof T>(method: K, ...args: Parameters<T[K]>): ReturnType<T[K]> {
    return (this as any)[method](...args);
  }
}

class Example extends Dynamic<Example> {
  greet(name: string): this {
    console.log(`Hello, ${name}`);
    return this;
  }

  farewell(name: string): this {
    console.log(`Goodbye, ${name}`);
    return this;
  }
}

const example = new Example();
example.call("greet", "Alice").call("farewell", "Bob");
// 输出:
// Hello, Alice
// Goodbye, Bob

在这里:

  • Dynamic 使用条件推断动态地扩展了 Example 的方法。

6. 实际应用场景

(1) 链式调用
  • 使用 this 类型实现流畅的链式调用。
示例
class QueryBuilder {
  private query: string = "";

  select(columns: string[]): this {
    this.query += `SELECT ${columns.join(", ")}`;
    return this;
  }

  from(table: string): this {
    this.query += ` FROM ${table}`;
    return this;
  }

  where(condition: string): this {
    this.query += ` WHERE ${condition}`;
    return this;
  }

  build(): string {
    return this.query;
  }
}

const query = new QueryBuilder()
  .select(["id", "name"])
  .from("users")
  .where("age > 18")
  .build();

console.log(query); // 输出 "SELECT id, name FROM users WHERE age > 18"

在这里:

  • 每个方法返回 this,支持链式调用。

(2) 动态类型推断
  • 使用泛型和条件类型动态地推断返回值类型。
示例
class State<T> {
  private state: T;

  constructor(initialState: T) {
    this.state = initialState;
  }

  setState<K extends keyof T>(key: K, value: T[K]): this {
    this.state[key] = value;
    return this;
  }

  getState(): T {
    return this.state;
  }
}

const state = new State({ name: "Alice", age: 30 })
  .setState("age", 31)
  .setState("name", "Bob");

console.log(state.getState()); // 输出 { name: "Bob", age: 31 }

在这里:

  • setState 方法动态地推断出键和值的类型。

7. 总结

  • this 类型的核心作用
    • 支持链式调用。
    • 在继承中实现多态。
  • 高级用法
    • 结合条件类型和泛型实现动态类型推断。
    • 动态扩展方法或属性。
  • 实际场景
    • 构建链式 API。
    • 动态类型推断和状态管理。

相关文章:

  • LeetCode 每日一题 2712. 使所有字符相等的最小成本 O(n)
  • 深度学习入门:从神经网络基础到简单实现
  • 国产人才画像系统哪家好?
  • Redis-15.在Java中操作Redis-Spring Data Redis使用方式-操作集合类型的数据
  • Java 大视界 -- Java 大数据机器学习模型在金融衍生品定价中的创新方法与实践(166)
  • 2025年- G34-Lc108-207. 课程计划--java版
  • Java面试黄金宝典25
  • 51单片机的五类指令(五)——位操作类指令
  • 身份证信息查询API:精准识别身份证号码
  • 操作系统高频(五)linux命令
  • 【小也的Java之旅系列】02 分布式集群详解
  • 经典算法Golang实现
  • 【含文档+PPT+源码】基于微信小程序的社交摄影约拍平台的设计与实现
  • python 语言始终是中文
  • 论文笔记(七十五)Auto-Encoding Variational Bayes
  • vue父子周期解决问题
  • Dubbo分布式框架学习(2)
  • Cursor:AI 驱动的编程变革者
  • JavaScript性能优化实践:从微观加速到系统级策略
  • 【数据分享】中国3254座水库集水区特征数据集(免费获取)
  • 费高云不再担任安徽省人民政府副省长
  • 超新星|18岁冲击中超金靴,王钰栋的未来无限可能
  • 高适配算力、行业大模型与智能体平台重塑工业城市
  • 海北州委常委、常务副州长桑本履新青海省供销社理事会主任
  • 武汉旅游体育集团有限公司原党委书记、董事长董志向被查
  • 道指跌逾100点,特斯拉涨近5%