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

推荐订阅源

AWS News Blog
AWS News Blog
Jina AI
Jina AI
量子位
V
V2EX
The GitHub Blog
The GitHub Blog
阮一峰的网络日志
阮一峰的网络日志
The Cloudflare Blog
博客园 - 【当耐特】
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
L
LangChain Blog
博客园_首页
aimingoo的专栏
aimingoo的专栏
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Check Point Blog
T
Tailwind CSS Blog
M
MIT News - Artificial intelligence
Engineering at Meta
Engineering at Meta
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
宝玉的分享
宝玉的分享
博客园 - 聂微东
G
Google Developers Blog
C
CERT Recently Published Vulnerability Notes
K
Kaspersky official blog
NISL@THU
NISL@THU
Hacker News: Ask HN
Hacker News: Ask HN
腾讯CDC
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
H
Hacker News: Front Page
Webroot Blog
Webroot Blog
有赞技术团队
有赞技术团队
W
WeLiveSecurity
Martin Fowler
Martin Fowler
S
Security @ Cisco Blogs
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
WordPress大学
WordPress大学
雷峰网
雷峰网
PCI Perspectives
PCI Perspectives
月光博客
月光博客
SecWiki News
SecWiki News
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 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