【发布时间】:2021-06-16 22:12:48
【问题描述】:
如何通过强制将type 导出为实例来导出它。
我尝试了很多方法,我只找到了一个解决方案,创建一个静态 getter,但我想删除我的静态 getter。
这里的上下文:
我想从 $A.A 那里导出 A 的一个实例类型,仅供参考。
export const $A = (() => {
class A {
static get default() {
return A.create();
}
static create() {
return new A();
}
constructor() {}
}
return { A };
})();
我尝试了很多方法,这里有 7 个!没有人按照 1 的方式工作!但这是因为我在 js 类中添加了一个静态 getter。
export type _1 = typeof $A.A.default;
export type _2 = typeof new $A.A;
export type _3 = typeof $A.A.create();
export type _4 = typeof $A.A();
export type _5 = typeof $A['A'];
export type _6 = $A.A;
export type _7 = typeof new ()=>$A.A;
// example somewhere in the project, i want tell A should be a instance and not a typeof!
function foo(A:_6)
那么在 ts 类型中模拟实例以导出到某处仅用于 typage 的语法是什么。 我的项目是在 js 中,但使用 ts 只是为了帮助 tsserver 在他不理解我的 refs 时。
- 所以它仅用于我的 ide 中的 Intelisence,而不用于生成 ts=>js。
【问题讨论】:
-
这里的泛型到底是什么?我在任何地方都看不到类型参数。
-
对不起,翻译小姐,我删除了
genericword -
参见例如this github issue,或this qeustion。您应该得到的相关错误消息是“Exported variable '$A' has or is using private name 'A'.(4025)”。
-
这与这个问题无关,但是上面的那个人找到了这个工具很好。也感谢您的宝贵时间。
标签: javascript typescript visual-studio-code type-conversion