Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TypeScript:用其他名称空间替换名称空间_Typescript_Tslint_Javascript Namespaces - Fatal编程技术网

TypeScript:用其他名称空间替换名称空间

TypeScript:用其他名称空间替换名称空间,typescript,tslint,javascript-namespaces,Typescript,Tslint,Javascript Namespaces,TSLint抱怨不应该使用名称空间,就我所理解的常识而言,它们不应该再被使用,因为它们是特殊的TypeScript构造 因此,我有一个简单的时间戳接口: export interface Timestamp { seconds: number | Long; nanos: number; } 由于接口中缺少静态函数,我使用名称空间来组织该功能,如下所示: export namespace Timestamp { export function now(): Timestamp {

TSLint抱怨不应该使用名称空间,就我所理解的常识而言,它们不应该再被使用,因为它们是特殊的TypeScript构造

因此,我有一个简单的时间戳接口:

export interface Timestamp {
  seconds: number | Long;
  nanos: number;
}
由于接口中缺少静态函数,我使用名称空间来组织该功能,如下所示:

export namespace Timestamp {
  export function now(): Timestamp {
    ...
  }
}
如果没有名称空间,您现在将如何建模?下面的结构看起来很难看,还有其他方法吗

export const Timestamp = {
  now: () => {
    ...
  }
}

所以,我检查了lib.es6.d.ts,它看起来像是一个“const object”,这才是真正的方法:

interface DateConstructor {
    ...
    now(): number;
    ...
}

declare const Date: DateConstructor;
有趣的是,下面的结构也起作用,我认为这是“干净”的方法:

export interface Timestamp {
  seconds: number | Long;
  nanos: number;
}

export class Timestamp {
  public static now(): Timestamp {
    ...
  }
}
要获得更明智的答案: