【发布时间】:2019-08-10 01:13:27
【问题描述】:
我正在玩 TypeScript 中的“多重继承”,或者更确切地说是对 mixin 有很好的理解。经过许多弯路,我发现最简单的方法是尽可能少地显式转换,并且能够创建如下所示的内容(完整的示例可以在此gist 中找到)。
我的问题是:为什么 TypeScript 允许我构建它,但是却无法为此创建声明文件?
export function TaggedMixin<Super extends Ctor>(superClass: Super) {
class Tagged extends superClass {
public static TAG_PUBLIC: string;
protected static TAG_PROTECTED: string;
private static TAG_PRIVATE: string;
public tag_public!: string;
protected tag_protected!: number;
private tag_private!: number;
}
return Tagged;
}
const Tagged = TaggedMixin(class {
public static ANON_PUBLIC: string;
protected static ANON_PROTECTED: string;
private static ANON_PRIVATE: string;
public anon_public!: string;
protected anon_protected!: number;
private anon_private!: number;
});
class TaggedClass extends Tagged {
constructor() {
super();
TaggedClass.ANON_PUBLIC;
TaggedClass.ANON_PROTECTED;
TaggedClass.TAG_PUBLIC;
TaggedClass.TAG_PROTECTED;
this.anon_public;
this.anon_protected;
this.tag_public;
this.tag_protected;
}
}
编辑:
该TS的错误无法创建声明文件:
Property 'tag_protected' of exported class expression may not be private or protected.ts(4094)
Property 'tag_private' of exported class expression may not be private or protected.ts(4094)
Property 'TAG_PROTECTED' of exported class expression may not be private or protected.ts(4094)
Property 'TAG_PRIVATE' of exported class expression may not be private or protected.ts(4094)
【问题讨论】:
-
您能详细说明一下“未能创建声明文件”是什么意思吗?您确定声明文件中包含您不
export的内容吗? -
我将错误输出添加到问题中。示例已完成,没有其他导出或导入的东西。
标签: typescript multiple-inheritance encapsulation mixins