首页 > 解决方案 > 如何在打字稿上声明类道具?

问题描述

这是来自火基地的代码:

class Fire { 
 ...
}

Fire.shared = new Fire(); // error on .shared
export default Fire;

我得到的错误是

'typeof Fire' 类型上不存在'属性'shared'

我如何在打字稿中解决这个问题?

标签: typescriptclass

解决方案


You need use the keyword static in front of the property name to declare them as class fields just like in a regular javascript

class Fire {
  static shared: Fire = new Fire()
  ...
}

推荐阅读