打字稿中的静态类

Aru*_*agi 11 javascript static node.js typescript

有没有办法在typescript,node.js中创建一个静态类

我想创建一个静态类来保留所有常量和字符串.

什么是最好的方法呢?

Dmi*_*tin 30

当然,您可以使用静态属性定义类:

export class Constants {
    static MAX_VALUE = 99999;
    static MIN_VALUE = 0;
}
Run Code Online (Sandbox Code Playgroud)

然后在需要时使用它:

import { Constants } from '../constants';
console.log(Constants.MAX_VALUE);
Run Code Online (Sandbox Code Playgroud)


kem*_*002 7

您可以将所需的变量和函数放在模块中,这意味着它不必实例化.

module constants {
   export var myValue = 'foo';

   export function thisIsAStaticLikeFunction () {
      console.log('function called');
   }
}

.....
console.log(constants.myValue);
Run Code Online (Sandbox Code Playgroud)

真的没有真正的静态类,但这非常接近复制它.