Partial classes
Add support of partial classes. Mixins is not the same, because it's run-time realization. Need compile realization, where partial classes will be combine into one before converting typescript to javascript.
//FileA.ts
partial class ClassA
{
constructor(public name: string) {}
public sayHello(): string { return "hi!"; }
}
//FileB.ts
partial class ClassA
{
public sayBye(): string { return "by!"; }
}
will be:
partial class ClassA
{
constructor(public name: string) {}
public sayHello(): string { return "hi!"; }
public sayBye(): string { return "by!"; }
}
+1
I have a proposal that should satisfy both of these use cases:
- You want to write methods of your own class in different places.
- You want to add new methods to someone else's class, e.g. a builtin.
For (2), normally you would just use the :: operator to get infix syntax.
But if you want an old class to be part of a new interface, or if you want to dynamically dispatch, you will need to actually modify the prototype.
interface ITimes {
times(n: number): number
}
Array implements ITimes {
times(n: number): number {
return this.length * n
}
}
// The compiler should check that all methods of ITimes and IOther are implemented.
Number implements ITimes, IOther {
times(n: number): number {
return this * n
}
other(): void {}
}
// The old types should typecheck with the new interface.
const x: ITimes = true ? [] : 0
x.times(3)
// The interface list can be empty. This essentially gives you partial classes.
MyClass implements {
anotherMethod(): void {}
}
(In the example I use string method names, but since these are builtin types, using symbols would be better once those can be typechecked.)
An improvement that this has over true partial classes is that the class has a single definition, and other places merely extend it. This means there's a clear place to import the class from and makes translation to JS easier.
:+1: I would love to see partial classes too.
What is the status of this feature?
This use case for example :
I have a Math package that define a class Set, with methods Set#add, 'Set#remove'
// math.ts
export partial class Set {
add(){}
remove(){}
}
I have an optional, heavy, Relational package that define classes Relation and Tuple.
This Relational package would also add a Set#product method.
// relational.ts
///<reference path="./../math/tsd.d.ts"/>
partial class Set {
product(){}
}
export class Relation(){}
export class Tuple(){}
Partial classes would allow me to compose classes in a more flexible way. If I don't want to use the heavy relational package, I can still use the basic Set functionality. Other partial classes will only come in and add extra functionality to the class.
I'm scratching my head to do something that will have a nice API without the use of partials in this case.
All I could come with is some repository pattern.
// Without partials
// base
export class Base {
static registerOperation(opName, method){
this.prototype[opName] = method;
}
operation(name, ...args){
return this[name].apply(this, args);
}
}
// math.ts
import {Base} from './base';
class Set extends Base{
// Define the methods here
add(){}
remove(){}
}
// Or here
Set.registerOperation("add", function(...){});
Set.registerOperation("remove", function(...){});
// relational.ts
import {Set} from './../math/math';
Set.registerOperation("product", function(...){});
// app.ts
import {Set} from 'math';
var set = new Set();
set.add // compiler error
set.remove // compiler error
set.product // compiler error
// have to use something like
set.operation("add", args);
// or
(<any>set).add(arg);
+1 I'm using t4 to generate classes and proxies over WebApi. I think auto-generation is using vastly in typescript and when it comes to auto-generation, partial classes is necessary!
It would be great for splitting aspects of the same class especially in React +1
+1 Code generation and "Just enough separation" in the case of pure reusable data objects with client-specific attributes for like validation that would be neat to have defined on the class but maintained in a separate piece of source code.
+1 for Code generation of partial classes and handcode additional members for these classes in separate files.
+1
+1 I have large base of methods in our js api service, it would be best to split them into files, which makes it easy to understand each method.
api service like fb (facebook) or gq (google analytics), where you are given one global class or object, which you can use through out your development.
+1
This feature would be a big plus for us too.
We developped a middleware where all types of clients can connect to, and communicate with our servers through it.
We generate some of the client code based on what is exposed by the servers.
So far, everything was ok. But now, we'd like to be able to transfer heterogeneous collections of objects (but with a same base class). The objects transfered are part of the code generated based on the server API.
To be able to use the power of inheritance, abstract methods are the key in this case. But our developpers won't add methods to generated code, i presume everyone knows why ;)
As far as i understood (i'm a C# developper), the solution proposed here (https://github.com/Microsoft/TypeScript/issues/9) would not allow us to do that.
So, partial classes would be perfect for us. We would generate the class as partial, and if developpers need to add logic or methods, they would just have to write an other part wherever they want.
+1
+1 very helpful for code generation
I wonder if module augmentation basically negates the need for this as a specific feature.
If you have a generated class then you should be able to do something like...
import {MyClass} from "./MyClass.generated"
MyClass.prototype.partialMethod1 = function() {
return true;
}
MyClass.prototype.partialMethod2 = function(abc: string) {
this.doSomething(abc);
}
declare module './MyClass.generated' {
interface MyClass {
partialMethod1(): boolean;
partialMethod2(abc: string): void;
}
}
@disshishkov That looks like a good way to implement it under the cover, but I'd still like proper first-class support in the language without the need to manually modify prototypes and maintaining that interface.
I agree with Elephant-Vessel. The manually crafted part of the partial class should be as loosly coupled (at design time in TS) as possible form the generated part.
The suggestion by @david-driscoll works for many cases. However, as far as I can tell, prototypes don't have access to internal variables. This limits the usefulness of this approach in code generation contexts where you want to generate the core parts of your classes and then augment them with custom code.
+1
@david-driscoll that looks like an excellent way to desugar this feature. (would need to add the ability to access to private/protected properties). Wonder what would happen with multiple partial class declaration? What would the mutual visibility of those be?
++ 1 Lack of partial is really hurting me when trying to extend my auto generated models..