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

推荐订阅源

F
Fortinet All Blogs
V2EX - 技术
V2EX - 技术
The Last Watchdog
The Last Watchdog
宝玉的分享
宝玉的分享
T
Tenable Blog
WordPress大学
WordPress大学
K
Kaspersky official blog
Microsoft Security Blog
Microsoft Security Blog
大猫的无限游戏
大猫的无限游戏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Hacker News - Newest:
Hacker News - Newest: "LLM"
P
Palo Alto Networks Blog
Help Net Security
Help Net Security
V
Vulnerabilities – Threatpost
Know Your Adversary
Know Your Adversary
C
CXSECURITY Database RSS Feed - CXSecurity.com
A
Arctic Wolf
Forbes - Security
Forbes - Security
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog
H
Hacker News: Front Page
W
WeLiveSecurity
博客园 - 【当耐特】
G
Google Developers Blog
Martin Fowler
Martin Fowler
TaoSecurity Blog
TaoSecurity Blog
Hacker News: Ask HN
Hacker News: Ask HN
人人都是产品经理
人人都是产品经理
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
AI
AI
N
Netflix TechBlog - Medium
C
Cisco Blogs
I
Intezer
aimingoo的专栏
aimingoo的专栏
博客园 - 聂微东
G
GRAHAM CLULEY
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
MyScale Blog
MyScale Blog
L
Lohrmann on Cybersecurity
Engineering at Meta
Engineering at Meta

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