Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I split a TypeScript class into multiple files?

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 partial declaration in C#.

like image 474
Adrian Rosca Avatar asked May 26 '14 19:05

Adrian Rosca


People also ask

Does TypeScript have partial classes?

Unfortunately, TypeScript doesn't support partial classes. And according to this thread, it will never support it.

What are Mixins in TypeScript?

Mixins are a faux-multiple inheritance pattern for classes in JavaScript which TypeScript has support for. The pattern allows you to create a class which is a merge of many classes. To get started, we need a type which we'll use to extend other classes from.

Does TypeScript have classes?

Classes in TypeScript, like JavaScript, are a special syntax for its prototypical inheritance model, which is comparable to inheritance in class-based object-oriented languages. Classes are just special functions added to ES6 that are meant to mimic the class keyword from these other languages.

Why no partial classes in typescript?

There was a feature request to implement partial classes, first on CodePlex and later on GitHub, but on 2017-04-04 it 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: TypeScript already has too many TS-specific class features [...]

How to extend bigclass in typescript?

import { BigClassBase } from './bigclassbase' class BigClass extends BigClassBase { methodTwo () { return 2; } } You can import BigClass in any other typescript file. but I can not extends more than 1 Class.

Why would you want to split a class into multiple files?

The main motivation behind looking into this was classes that could potentially get large and wanting to split those files up into smaller more digestable files. With this approach the class functions can be put into as many different files as need be and collected in the file where the class is declared.

What is the use of TSC in typescript?

tsc: It stands for TypeScript compiler which is used to invoke the compiler in order to compile the TypeScript files. –out: It is a CLI (Command Line Interface) command which concatenates the TypeScript files and emits the output to a single JS file.


2 Answers

Lately I use this pattern:

// file class.ts import { getValue, setValue } from "./methods";  class BigClass {     public getValue = getValue;     public setValue = setValue;      protected value = "a-value"; } 
// file methods.ts import { BigClass } from "./class";  function getValue(this: BigClass) {     return this.value; }  function setValue(this: BigClass, value: string ) {    this.value = value; } 

This way we can put methods in a seperate file. Now there is some circular dependency thing going on here. The file class.ts imports from methods.ts and methods.ts imports from class.ts. This may seem scary, but this is not a problem. As long as the code execution is not circular everything is fine and in this case the methods.ts file is not executing any code from the class.ts file. NP!

You could also use it with a generic class like this:

class BigClass<T> {     public getValue = getValue;     public setValue = setValue;      protected value?: T; }  function getValue<T>(this: BigClass<T>) {     return this.value; }  function setValue<T>(this: BigClass<T>, value: T) {     this.value = value; } 
like image 95
Elmer Avatar answered Sep 30 '22 08:09

Elmer


You can't.

There was a feature request to implement partial classes, first on CodePlex and later on GitHub, but on 2017-04-04 it 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:

TypeScript already has 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.

like image 23
user247702 Avatar answered Sep 30 '22 08:09

user247702