惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
The Exploit Database - CXSecurity.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Spread Privacy
Spread Privacy
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
S
Security Affairs
Help Net Security
Help Net Security
Simon Willison's Weblog
Simon Willison's Weblog
N
News and Events Feed by Topic
P
Palo Alto Networks Blog
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
N
News and Events Feed by Topic
C
Cyber Attacks, Cyber Crime and Cyber Security
TaoSecurity Blog
TaoSecurity Blog
T
Tor Project blog
Schneier on Security
Schneier on Security
WordPress大学
WordPress大学
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
V
Visual Studio Blog
F
Fortinet All Blogs
大猫的无限游戏
大猫的无限游戏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tenable Blog
M
MIT News - Artificial intelligence
T
Threatpost
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
News | PayPal Newsroom
P
Privacy & Cybersecurity Law Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Microsoft Azure Blog
Microsoft Azure Blog
小众软件
小众软件
Cyberwarzone
Cyberwarzone
雷峰网
雷峰网
云风的 BLOG
云风的 BLOG
量子位
Google DeepMind News
Google DeepMind News
O
OpenAI News
爱范儿
爱范儿
H
Hackread – Cybersecurity News, Data Breaches, AI and More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
F
Full Disclosure
B
Blog
Cloudbric
Cloudbric
MyScale Blog
MyScale Blog
阮一峰的网络日志
阮一峰的网络日志

博客园 - kingBook

git patch git 修改最后一次提交的日期 Win10 修改特定格式文件的右键快捷菜单 TypeScript async、 await、Promise LayaAir3.x 侦听程序退出 旋转力学例子 旋转力学公式 凹多边形碰撞检测 LayaAir3.x 侦听键盘事件 URP 阴影 TypeScript 里的 override Unity 二维数组序列化 C# 匿名对象、动态属性 Cocos Creator 安卓模拟器中无法运行 Unity Editor 保存图片、缩放纹理 LayaAir3.2.0-beta.2 设置2d刚体线性速度,在不同设备(分辨率)下,表现不一致的问题 LayaAir3.x 物理2D碰撞事件 TypeScirpt 声明Map类型变量 TypeScript 声明函数类型变量
TypeScript 类的自身类型
kingBook · 2024-10-25 · via 博客园 - kingBook

需要定义一个 class 类型的非实例变量时,可以用以下格式:

  • 变量名 : typeof 类名; class 定义了有参数的构造函数时,不可用
  • 变量名 : new() => 类名;
  • 变量名 : { new(): 类名 };
    当 class 定义了有参数的构造函数时,也需要对应:
  • 变量名 : new(name:string) => 类名;
  • 变量名 : { new(name:string): 类名 };
export class Animal { }

export class Dog extends Animal { }

export class Fox extends Animal {

    public constructor(name: string) {
        super();
    }

}

export class Test {

    private _animalType1: { new(): Animal };
    private _animalType2: new () => Animal;
    private _animalType3: typeof Animal;
    private _animalType4: new (name: string) => Animal;
    private _foxType: typeof Fox;

    public constructor() {
        this._animalType1 = Animal;
        this._animalType1 = Dog;
        //this._animalType1 = Fox; // 编译错误:Type 'typeof Fox' is not assignable to type 'new () => Animal'

        this._animalType2 = Animal;
        this._animalType2 = Dog;
        //this._animalType2 = Fox; // 编译错误:Type 'typeof Fox' is not assignable to type 'new () => Animal'

        this._animalType3 = Animal;
        this._animalType3 = Dog;
        //this._animalType3 = Fox; // 编译错误:Type 'typeof Fox' is not assignable to type 'new () => Animal'

        this._animalType4 = Animal;
        this._animalType4 = Dog;
        this._animalType4 = Fox; // 编译通过

        this._foxType = Fox;

        //this.getName1(Fox); // 编译错误:Argument of type 'typeof Fox' is not assignable to parameter of type 'typeof Animal'
        //this.getName2(Fox); // 编译错误:Argument of type 'typeof Fox' is not assignable to parameter of type 'typeof Animal'
        //this.getName3(Fox); // 编译错误:Argument of type 'typeof Fox' is not assignable to parameter of type 'typeof Animal'
        this.getName4(Fox); // 编译通过
        this.getName5(Fox); // 编译通过
        //this.getName6(Fox); // 编译错误:Argument of type 'typeof Fox' is not assignable to parameter of type 'new () => Fox'.
        //this.getName7(Fox); // 编译错误:Argument of type 'typeof Fox' is not assignable to parameter of type 'new () => Fox'
        this.getName8(Fox); // 编译通过
        this.getName9(Fox); // 编译通过
    }

    public getName1(type: { new(): Animal }): string {
        return type.prototype.constructor.name;
    }

    public getName2(type: new () => Animal): string {
        return type.prototype.constructor.name;
    }

    public getName3(type: typeof Animal): string {
        return type.prototype.constructor.name;
    }

    public getName4(type: new (name: string) => Animal): string {
        return type.prototype.constructor.name;
    }

    public getName5(type: { new(name: string): Animal }): string {
        return type.prototype.constructor.name;
    }

    public getName6<T extends Animal>(type: new () => T): string {
        return type.prototype.constructor.name;
    }

    public getName7<T extends Animal>(type: { new(): T }): string {
        return type.prototype.constructor.name;
    }

    public getName8<T extends Animal>(type: new (name: string) => T): string {
        return type.prototype.constructor.name;
    }

    public getName9<T extends Animal>(type: { new(name: string): T }): string {
        return type.prototype.constructor.name;
    }

    // 此方法不可用
    // 编译错误:T' only refers to a type, but is being used as a value here.
    //public getName10<T extends Animal>(type: typeof T): string {
    //    return type.prototype.constructor.name;
    //}

}

参考:
https://typescript.p6p.net/typescript-tutorial/class.html