如何将 TypeScript 类拆分为多个文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23876782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I split a TypeScript class into multiple files?
提问by Adrian Rosca
I found a lot of examples and also tried myself to split a module into several files. So I get that one, very handy. But it's also practical sometimes to split a class for the same reason. Say I have a couple of methods and I don't want to cram everything into one long file.
找了很多例子,自己也试着把一个模块拆分成几个文件。所以我得到了那个,非常方便。但有时出于同样的原因拆分班级也很实用。假设我有几种方法,我不想把所有东西都塞进一个长文件中。
I'm looking for something similar to the partialdeclaration in C#.
我正在寻找类似于C# 中的部分声明的东西。
采纳答案by user247702
You can't.
你不能。
There was a feature request to implement partial classes, first on CodePlexand later on GitHub, but on 2017-04-04it was declared out-of-scope. A number of reasons are given, the main takeaway seems to be that they want to avoid deviating from ES6 as much as possible:
有一个实现部分类的功能请求,首先是在CodePlex 上,后来是在GitHub 上,但在 2017 年 4月 4日被宣布超出范围。给出了许多原因,主要的收获似乎是他们希望尽可能避免偏离 ES6:
TypeScript alreadyhas too many TS-specific class features [...] Adding yet another TS-specific class feature is another straw on the camel's back that we should avoid if we can. [...] So if there's some scenario that really knocks it out of the park for adding partial classes, then that scenario ought to be able to justify itself through the TC39 process.
TypeScript已经有太多特定于 TS 的类功能 [...] 添加另一个特定于 TS 的类功能是骆驼背上的另一根稻草,我们应该尽可能避免。[...] 因此,如果有一些场景确实因为添加部分类而无法实现,那么该场景应该能够通过 TC39 流程来证明自己的合理性。
回答by Elmer
Lately I use this pattern:
最近我使用这种模式:
class BigClass {
public getValue = getValue;
public setValue = setValue;
protected value = "a-value";
}
function getValue(this: BigClass) {
return this.value;
}
function setValue(this: BigClass, value: string ) {
this.value = value;
}
Often I move the functions in seperate files, for a clean and scalable setup!
我经常在单独的文件中移动函数,以获得干净和可扩展的设置!
回答by Elmer
I use this (works in typescript 2.2.2):
我使用这个(在打字稿 2.2.2 中有效):
class BigClass implements BigClassPartOne, BigClassPartTwo {
// only public members are accessible in the
// class parts!
constructor(public secret: string) { }
// this is ugly-ish, but it works!
methodOne = BigClassPartOne.prototype.methodOne;
methodTwo = BigClassPartTwo.prototype.methodTwo;
}
class BigClassPartOne {
methodOne(this: BigClass) {
return this.methodTwo();
}
}
class BigClassPartTwo {
methodTwo(this: BigClass) {
return this.secret;
}
}
回答by masp
I use plain subclassing when converting large old multi file javascript classes which use 'prototype' into multiple typescript files:
将使用“原型”的大型旧多文件javascript类转换为多个打字稿文件时,我使用普通子类化:
bigclassbase.ts:
bigclassbase.ts:
class BigClassBase {
methodOne() {
return 1;
}
}
export { BigClassBase }
bigclass.ts:
bigclass.ts:
import { BigClassBase } from './bigclassbase'
class BigClass extends BigClassBase {
methodTwo() {
return 2;
}
}
You can import BigClass in any other typescript file.
您可以在任何其他打字稿文件中导入 BigClass。
回答by Masih Jahangiri
We can extend class methods gradually with prototype
and Interface
definition:
我们可以使用prototype
和Interface
定义逐步扩展类方法:
import login from './login';
import staffMe from './staff-me';
interface StaffAPI {
login(this: StaffAPI, args: LoginArgs): Promise<boolean>;
staffsMe(this: StaffAPI): Promise<StaffsMeResponse>;
}
class StaffAPI {
// class body
}
StaffAPI.prototype.login = login;
StaffAPI.prototype.staffsMe = staffsMe;
export default StaffAPI;