Repository navigation
Partial classes #563
Description
Activity
I think you employ "partial" on modules too and help resolve this issue raised here.
Dis Shishkov (@disshishkov) perhaps extension methods be sufficient : #9
RyanCavanaugh commented
on Aug 29, 2014 MemberMore actionsWhat kind of situations do you want to use this for, and how do existing solutions fall short of that?
Note that this was implemented in C# to support WinForms-type editing scenarios where you have an auto-generated file and a user-edited file contributing to the same type; I'm not sure those kind of situations apply in JavaScript/TypeScript.
Reacted by Gabriele and Pekka SavolainenReacted by João Vitor Paes de Barros do Carmo and JeeShenSome classes can has many line numbers, just for better read splitting to several separate classes will help. You can split by different type, for example by logic (in case where this logic can not moved to another class), by visibility (private and public) and other. In case when combing of partial classes can has problems (for example 2 partial classes same the same method/variable declaration) compiler should notify and throw error.
RyanCavanaugh commented
on Aug 29, 2014 MemberMore actionsThis isn't really compelling. If your class is so large it can't be comfortably edited in a single file due to its sheer size, it's a very major design smell (e.g. comments in http://programmers.stackexchange.com/questions/157482). Splitting by different type/logic/visibility/etc is something that could be handled by an IDE or by organization within a single file.
It's worth discussing why your classes are so big they can't be navigated (is it because the navigation tools should be better?), and if there are better ways the language could support decomposing a class rather than just splitting it.
Reacted by Sean Vieira, Alex, David Young, Tingan Ho, Pekka Savolainen, AA, Nick Fisher and Trotyl YuPersonally, I think "partial classes" should exist, but behave like modules that merge together. I have a system with modules that, although intellisense sees all modules (as it should), the actual JS containing it is only loaded when needed. I think that same would be great for classes as well - to only load the parts needed. I've also wanted to create a function, and use a class to further expand on it. Currently, you can only do this with modules.
A use case for partial classes is for generated proxy classes. E.g. WebAPI or SignalR wrappers. It would be really nice to be able to extend the generated proxy classes with custom logic. Especially when generating classes for models it would be nice to be able to attach business logic directly to the model classes returned from the api.
Reacted by Amel Musić, James Manning, Johan Benschop, Nicolas Kyriazopoulos-Panagiotopoulos, Rhobal, Anshul Vishwakarma, Benjamin Meyer, Vetési Zoltán, greendimka, Kiryl and 5 more+1 kvantetore.
The use case is exactly the same as .net; a portion of the class is generated (in our case, from Avro schemas) and you want to add additional helper code for working with the generated classes.
I like this suggestion. I would like to have partial classes in order to separate attributes/properties of classes forming my "data hierarchy" which could be gathered in one single file, from their methods which can be split in several other files. It would make code clearer and easier to understand in one glance imo.
My data hierarchy file:
class A { x: number; y: number; z: number; } class B extends A { value: string; flag1: boolean; flag2: boolean; }
File containing methods of class A:
class A { constructor(x: number, y: number, z: number) { this.x = x; this.y = y; this.z = z; } method1(...) { ... } ... methodN(...) { ... } }
File containing methods of class B:
class B extends A { constructor(x: number, y: number, z: number, value: string) { super(x, y, z); this.value = value; this.flag1 = false; this.flag2 = false; } method1(...) { ... } ... methodN(...) { ... } }
+1 from me. I would like to use tt templates to generate Typescript classes, but add to them in a separate file that won't get replaced by the template generation.
Reacted by Petr Gusev, Arturo Martinez, James Manning, Anderson Fortaleza, Roman Pokrovskij, Franklin Davenport, Stefan Zettel, Rushui Guan, Bruno Alexandre and JihedHalimi+1 This will really help me with my auto generated classes that I need to extend
Partial Class is really nice but it doesn't make sense to have it. It showed you have a design problem. Rule of thumb in programming is to keep it simple stupid (KISS), regardless of languages you use, is to break apart large class into smaller ones - by either 1) splitting off scripts into classes and call them (Other classes can also call that smaller classes too instead of re-inventing the wheel), or 2) break apart & convert into Abstract/Interface/Inheritance/Virtual etc. or polymorphism.
Let's say you have a Vehicle with everything in it. A partial class doesn't make sense and it introduce complexity & overhead here where it makes more sense to do this instead. Create an Engine class, Tranny class, Drivetrain class, Door class & Tire class as seperate classes and move scripts over to them, that when called can still define a Vehicle within the Vehicle class. It reduce lengthy scripts & script complexity. Chance are you will find you have Door scripts here and there which can be simplified by a Door class. Interface/Abstract/Inheritance/Virtual can be use to alter the Door class definition on some scripts in the Vehicle class.
You're also more likely to have less develoment time this way. I once had the ASP.NET C# Blog that use lots of partial classes. I had struggled with it cuz of too many partial classes and you don't know where they are. It is sort of like dealing with GOTO logic in programming. Bad bad!! I was never able to create a patch for the bugfix successfully, nor was I able to customize the script cuz it was too buried in pile of partial classes (so do some renamed wording).
Just saying it as a 1 cent thought from my mind.
Reacted by johnny and Aluan HaddadReacted by Louis-Dominique Dubeaufletchsod-developer: Some situations are properly handled by inheritance, but not all. And some developers may misuse partial classes, but not all. There are some situations where I find partial classes very, very useful, but if you don't like them, you don't have to use them.
206 remaining items
Load more actionsaluanhaddad commented
on Apr 10, 2017 ContributorMore actionsI 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 :)
aluanhaddad commented
on Apr 11, 2017 ContributorMore actionsgreendimka 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.Reacted by Ken HaddenAluan Haddad (@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.
Reacted by Trotyl Yu, jwbay and Corbin UseltonKen Hadden (@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.
Reacted by Aluan Haddad and Corbin Useltonaluanhaddad commented
on Apr 11, 2017 ContributorMore actionsspion (@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
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.
Aluan Haddad (@aluanhaddad) why do you keep making this out as a run-time thing? Module loaders and all that have nothing to do with transpiling.
+1
Reacted by Trotyl Yualuanhaddad commented
on May 3, 2017 ContributorMore actionsAluan Haddad (@aluanhaddad) why do you keep making this out as a run-time thing? Module loaders and all that have nothing to do with transpiling.
That is not correct.
Look at the SystemJS and Webpack ecosystems and you will see otherwise.
Even more traditional, and rock solid gulp workflows rely on the correspondence between input and output files.
+1
I need partial class because we are generating most of the base class using tools (to automate when there's a changes in the model). We then use partial class to add functionality to the class in a separate files so it won't be overwrite by the tool (that auto-generate the class).
- locked and limited conversation to collaborators
on May 10, 2017
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.
will be: