首页 > 解决方案 > 在 Typescript 的类声明中使用“declare”关键字的目的是什么?

问题描述

我在vscode源代码中看到了一些代码,并将其重写为一个简单的版本。

我想知道在下面的代码中使用声明的目的:

interface IDog {
    readonly color: string
}

class Doggy {
    public color = "";
}

class Doggy2 implements IDog {
    //Here is the code as mentioned
    declare readonly color: string;
}

const aaa = new Doggy()
const bbb = new Doggy2()

在编译的代码中,Doggy2类中的颜色被忽略了。这是declare的正常用法吗?我在哪里可以在 Typescript 中学习这些特殊代码?

标签: typescript

解决方案


推荐阅读