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!"; }
}
@RyanCavanaugh
Perhaps so... Perhaps they don't make as much sense... But like I have said numerous times, I think even in this thread, partial classes are all about making code generation easier... That's the main benefit I see from them worth the feature in TS... I, and I have to assume many others but that's just a guess, am currently generating tons of TS code from my C# code... But then the question becomes how to safely extend the generated TS code??? This is a far easier question to answer with partial classes... Currently we are resorting to hacks in order to keep custom code in our generated TS code files...
Thanks
Same thing here, we generate the typescript "proxy" for our C# APIs, and we'd like to be able to extend these objects in typescript easily.
I'm not a JS specialist so i must be missing something, because, i don't see why being able to split the declaration of classes in multiple TS files (with only one class declaration in only one JS file after compilation) would be a problem for the JS world. As far as i see it, it's transparent for JS, as if it would have been written in one file from the begining. I mean, if you use partial classes or not, the JS output will be exactly the same, but it will prevent the cost of JS object extensibility for our developpers.
@RyanCavanaugh
One other thing I wanted to mention that me personally I am not asking the TS compiler to somehow solve the packaging dilemma that might arise when creating an ES6 module using partial classes in different TS files... It makes perfect sense to me at least to compile multiple TS files into a single js file for the ES6 module... Again, partial classes helps code generation significantly... They allow tooling to create large parts of your model with reliable extensibility...
@kitsonk
You keep asking for a specific language feature, as in "I would love a green car" but aren't getting to the crux of the issue, why does the car need to be green? If you explained why your car needs to be green, or be open to the fact that you don't actually care what colour your car is, as long as it provides for your real needs.
No, I'm not asking for a new feature anymore. It has been stated over and over that the language can accomplish what I want. I've provided examples of what I want (in C#), but I have yet to see in the helpful examples a way to get to where I need to be at.
I don't see why you feel it is Ryan's obligation to tell you how to repaint your car, just because for valid reasons the car company has decided not to produce green cars.
Ryan seems to be a moderator here and is closing the issue. That's why. If he is a language authority, then perhaps he shouldn't close the issue before making an effort to understand my need and dismissing this request without providing example of how to do it with native language features. I've stated that I'd like a compile-time solution. And I'm a pretty good developer and have been around for a while programming in many different and obscure languages, and I still have a really hard time with the mixin syntax. I don't get how something that complex and hard to understand got into the language, and yet everyone is rejecting the elegance of partial class syntax, simply, it seems, because it's borrowed from C#.
Ryan seems to be a moderator here and is closing the issue. That's why. If he is a language authority, then perhaps he shouldn't close the issue before making an effort to understand my need
🤔
@RyanCavanaugh, I know you've made an effort to help. And at this point I'm moving on to try mixins. Or just inheritance, or maybe I'm in such a hurry to get this project done that I'm overlooking a way to do this with generics. Hmm... With inheritance and naming, I can do:
class AddressBase // this is a code generated class
{
public address: string;
public city: string;
public state: string;
public zip: string;
constructor( jsonFromService: any )
{
this.OnInit( jsonFromService );
}
OnInit( jsonFromService: any )
{
// could use Object.assign here
this.address = jsonFromService.address;
this.city = jsonFromService.city;
this.state = jsonFromService.state;
this.zip = jsonFromService.zip;
}
}
class ContactBase // this is also a code generated class
{
public firstName: string;
public lastName: string;
constructor( jsonFromService: any )
{
this.OnInit( jsonFromService );
}
OnInit( jsonFromService: any )
{
// could use Object.assign here
this.firstName = jsonFromService.firstName;
this.lastName = jsonFromService.lastName;
}
}
// classes that extend the functionality of the code generated classes:
class Address extends AddressBase // subclass simply because I don't want to have to use AddressBase all over my codebase, and then refactor if I ever extend the class
{
}
class Contact extends ContactBase
{
public Addresses: Address[] = []; // THIS is the customization/extension that cannot be code generated.
OnInit( jsonFromService: any )
{
// note that jsonFromService receives a dto with a array of address info
super.OnInit( jsonFromService );
for ( let addr of jsonFromService.Addresses )
{
this.Addresses.push( new Address( addr ) );
}
}
}
And that works fine for me. It's not as pretty and forces my code generated classes to have names that I don't want to use in code. But, again it works.
At the moment I feel that guys, who are strongly against partial classes, simply misuderstand the concept of partial classes, and they think that partial classes are something what partial classes are not.
To make it clear: partial class is a syntactical construction, which lets you split the definition of a same single class into several physical files. Keywords here are "same" and "single".
TypeScript's documentation gives bad definition of partial classes (first paragraph here: https://www.typescriptlang.org/docs/handbook/mixins.html).
Example of real partial classes concept:
File "Point.generated.ts":
partial class Point {
readonly x: number;
readonly y: number;
}
File "Point.codeByHand.ts":
partial class Point {
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
translate(dx: number, dy: number): Point
{
return new Point(this.x + dx, this.y + dy);
}
}
They compile into "Point.js" - and the name comes from the name of a class, not from the names of files:
var Point = (function () {
function Point(x, y) {
this.x = x;
this.y = y;
}
Point.prototype.translate = function (dx, dy) {
return new Point(this.x + dx, this.y + dy);
};
return Point;
}());
Mixins, when used as "partial classes" (and I strongly believe that this term in documentation is misused) actually represent a multiple inheritance (https://en.wikipedia.org/wiki/Multiple_inheritance).
So, as we see, we discuss two different tools: real partial classes and multiple inheritance. These are two different tools. And as any tool - they have their own area of application. In other words: a carpenter may use a hammer instead of a mallet, when the mallet is not available, but the hammer is not the right tool.
IMHO, this discussion has no future and we should stop it. While I still see no technological problems for real partial classes, the team really has no wish to implement them, at least for now.
The TS team does not want to implement TS-specific features if possible. If you do want to discuss more then here is a better place: https://esdiscuss.org/topic/class-syntax-enhancements
I hope there is a GitHub repo on TC39 side to discuss this things rather than mailing based one :/
@greendimka
At the moment I feel that guys, who are strongly against partial classes, simply misuderstand the concept of partial classes, and they think that partial classes are something what partial classes are not.
To make it clear: partial class is a syntactical construction, which lets you split the definition of a same single class into several physical files. Keywords here are "same" and "single".
I don't think anyone here is misunderstanding the definition of partial classes, I think you're perhaps misunderstanding the definition of classes in JavaScript.
If you are under the impression that JavaScript classes are at all like the classes found in virtually any other widely used language, then it might well appear to you that others others are misunderstanding.
However, as has been said multiple times in this discussion, I know I've said it explicitly at least once, JavaScript classes are not declarative constructs they are imperative.
They do not exist until statements are executed.
Defining them relies on mutation.
This already makes them brittle and order dependent when it comes to inheritance.
This could go on and on...
@cosmoKenney
I don't get how something that complex and hard to understand got into the language, and yet everyone is rejecting the elegance of partial class syntax, simply, it seems, because it's borrowed from C#.
I really don't think that is correct. Everyone loves C#, even Java programmers 😛, and TypeScript is not different for the sake of being different.
@aluanhaddad
I think you're perhaps misunderstanding the definition of classes in JavaScript...
For citated and below: I am talking about the TypeScript. As I said: I do not see any technical problem implementing real partial classes in TypeScript, which will be compiled into constructs of JavaScript.
@greendimka
For citated and below: I am talking about the TypeScript. As I said: I do not see any technical problem implementing real partial classes in TypeScript, which will be compiled into constructs of JavaScript.
The problem with this satatement there is no such thing as a TypeScript class.
Oh, boy! Some people will use anything as a reason. Ok, there are no classes in TypeScript. Or TypeScript classes. Or whatever. Whatever.
I mean that they are JavaScript classes. While I am at times guilty of being pedantic, I'm very specifically making this distinction because we need to agree on the definition of the term class before we can discuss what features are simple to add to them.
I think we don't mean the same thing when we speak of classes and that causes cognitive dissonance and impedes mutual understanding.
I think we don't mean the same thing when we speak of classes and that causes cognitive dissonance and impedes mutual understanding.. Totally agree!
In fact the first post in this topic perfectly describes what was requested, therefore this discussion has no future :)
@greendimka yes, it has no future as it seems TypeScript will never have partial classes. In my view, that is a good thing.
However, misunderstandings and miscommunications are not good things which is why I am still conversating with you.
Unfortunately, you do not seem to be interested in either
A. Explaining what JavaScript are classes are that one might might change their mind and come to agree with you that they can trivially be made partializable.
B. Learning from others about what JavaScript classes are so that you must understand the opposing point of view.
I think that this is a shame, but I will not discuss it further if you so wish.
I am not against any discussions. But it has been stated several times here that partial classes will not be implemented (at least not now). So I think it is better to concentrate on other things. Maybe TypeScript can be changed in the future, who knows. In relation to A and B. I hate JavaScript. But I know it very well and, of course, I know how classes are "represented" in it. But the point of this whole discussion was not to modify the JavaScript, but to improve abilities of TypeScript, as a higher language, which produces "low-level" JavaScript code. Imagine we would replace TypeScript with C++ and JavaScript with machine code. Machine code lacks most concepts which exist in C++. But should C++ stop evolving because of it? Of course no. Should it stop evolving because some old compilers will not be able to compile? Of course no - you give a new feature to developers and tell them: it works with new compilers - you (developers) decide if you want to use it.
@aluanhaddad Man oh man. Who cares about the Javascript part of the equation? TypeScript is a layer above, or however you want to phrase it. Remember the old 4GL vs. 3GL discussions? TypeScript is to Javascript as a 4GL is to a 3GL, kind of. Also the argument that TypeScript is ES6 with strong types, thus Partial Classes is outside the scope of TypeScript's roadmap is LAME. We got Mixins, Generics, Modules, Name Spaces and Type Casting. So why not go the extra mile with Partial Classes?
All we want out of partial classes is the syntactic sugar that enables us to amalgamate all the various definitions of a single TypeScript class into one final - low level - 3GL - Javascript class definition. There should be no impact on the final JavaScript class definition, right? Why is the final product of the transpilation to Javascript even part of this discussion? Seriously.
@cosmoKenney "its just ES.next with types" is a big selling point that converts JavaScript developers. If you want something more, you're looking at the wrong language. Maybe try Scala.js instead?
edit: I just had an interesting realisation that in ES.next partial classes can be implemented with a custom module loader. If you use
import {MyClass} from './myclass.*'
the loader could merge all exported MyClass definitions from any files matching the wildcard into a single class, then provide that.
@spion I like how you are quite correctly referring to them as ES.next partial classes. An ES module loader, such as a browser, or loader polyfill, such as SystemJS, would then support partial classes. One of the primary problems with adding this feature at the TypeScript level is that it breaks existing tools such as loaders and packagers. On the other hand, if ECMAScript specified them, then all of these tools would implement the feature and TypeScript would remain compatible with all of these tools.
I definitely agree with you regarding Scala.js
@cosmoKenney
We got Mixins, Generics, Modules, Name Spaces and Type Casting. So why not go the extra mile with Partial Classes?
Mixins as seen in TypeScript are an ECMAScript design pattern that TypeScript types.
Generics are a type system feature so they do not apply.
Modules are an ECMAScript feature.
Namespaces are a syntactic sugar for and a formalization of an ECMAScript design pattern.
Type Casting does not exist in TypeScript.