gpt4 book ai didi

TypeScript:用其他东西替换命名空间

转载 作者:行者123 更新时间:2023-12-05 08:11:48 27 4
gpt4 key购买 nike

TSLint 提示不应该使用命名空间,据我所知,常识是不应该再使用它们,因为它们是特殊的 TypeScript 构造。

所以,我有一个简单的时间戳接口(interface):

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

由于接口(interface)中缺少静态函数,我使用 namespace 来组织该功能,如下所示:

export namespace Timestamp {
export function now(): Timestamp {
...
}
}

如果没有 namespace ,您将如何建模?下面的构造看起来很丑,还有别的办法吗?

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 {
...
}
}

关于TypeScript:用其他东西替换命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50406306/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com