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!"; }
}
Inheritance is messier and has more overhead.
On Mon, Aug 10, 2015, 10:09 AM Gorgi Kosev [email protected] wrote:
Why not use inheritance for code generation?
— Reply to this email directly or view it on GitHub https://github.com/Microsoft/TypeScript/issues/563#issuecomment-129468291 .
Inheritance creates useless name couplings
Inheritance is less awesome because it:
- Deepens the prototype chain needlessly and slows down instantiation, although thats not a big deal for most apps, likely everything but games;
- For lack of abstract and override either loses some type safety or introduces the need for some ackward patterns like casting between the two types;
- Is confusing overall as having a base class communicates the idea that it can be extended by other classes, which is rarely the case with codegen
Not saying typescript HAS to have partial classes to support codegen, only that partial classes are so much better at it than inheritance or composition.
@JoshMcCullough Inheritance overhead in JS is minimal, and I don't see how its messier than partial classes: with those you can potentially split a class into 10 files and chase your methods all over the place.
@yahiko00 What kind of "useless name coupling" are we talking about here? Do you refer to the things you'd need to do to get No. 2 below (hardcoding the name of the extended class)?
@hdachev
- There is some overhead, but its quite minimal - only noticeable if you're doing things that translate directly to a couple of machine instructions
- Good point - you don't get the extended class when you have to reference the generated class from other generated classes.
- Those classes are indeed meant to be extended - otherwise you could generate them and be done with it, right?
AFAIK this is not on the ES7+ radar, is it? That might be problematic...
In any case, attaching extra things to the prototype of an existing class is quite trivial in JavaScript and not at all uncommon, so it might be a good idea to have syntax for that...
It's straightforward in TS but the JS behind the scenes would double the "protoyping" if you were using base classes for all of your generated data objects. Probably not a serious performance impact unless you have a ton of data objects, but it still is unnecessary when we know that "partial classes" are possible.
On Mon, Aug 10, 2015 at 11:03 AM Gorgi Kosev [email protected] wrote:
@JoshMcCullough https://github.com/JoshMcCullough Inheritance overhead in JS is minimal, and I don't see how its messier than partial classes: with those you can potentially split a class into 10 files and chase your methods all over the place.
@yahiko00 https://github.com/yahiko00 What kind of "useless name coupling" are we talking about here? Do you refer to the things you'd need to do to get No. 2 below (hardcode the name of the extended class)?
class MyClass extends GeneratedMyClass { ... }
@hdachev https://github.com/hdachev
There is some overhead, but its quite minimal https://jsperf.com/prototype-chain-vs-direct-calls - only noticeable if you're doing things that translate directly to a couple of machine instructions 2.
Good point - you don't get the extended class when you have to reference the generated class from other generated classes. 3.
Those classes are indeed meant to be extended - otherwise you could generate them and be done with it, right?
AFAIK this is not on the ES7+ radar, is it?
In any case, attaching extra things to the prototype of an existing class is quite trivial in JavaScript and not at all uncommon, so it might be a good idea to have syntax for that...
— Reply to this email directly or view it on GitHub https://github.com/Microsoft/TypeScript/issues/563#issuecomment-129483174 .
@Davidhanson90 that file has zero classes in it. Did you mean to post this in a different thread?
I'll post onto the correct thread. https://github.com/Microsoft/TypeScript/issues/447
+1
+1
+100
+1
+1
+1 for code generation. I have been getting into angularjs and webapi and I want to make a tool that essentially creates automatic JS definitions from my c# objects and services. I want to then be able to extend these classes without having to edit the "scaffolded" JS definitions. I've see that a few other people are requesting this and it seems like we have similar use cases.
+1 for code generation
+1 for code generation, angular has lots of boilerplate code, we're already doing lots of generation of it, but with partial classes we would be able to do much more.
+1 yet again for code generation. Trying to extend classes in another file is messy at best.
@RyanCavanaugh, what is needed to get this out of "+1" mode and moving in a productive direction?
-1 a following syntax is better for mixins: #2919
Not sure why can't we have both partial classes and the mixins; two totally unrelated features.
+1
This would be nice when code is generated, but you don't want to modify it to add things to the generated code.