Skip to content

Partial classes  #563

Description

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!"; }
}

Activity

  1. Davidhanson90 commented on Aug 29, 2014

    @Davidhanson90

    I think you employ "partial" on modules too and help resolve this issue raised here.

    #447

  2. basarat commented on Aug 29, 2014

    @basarat
    Contributor

    Dis Shishkov (@disshishkov) perhaps extension methods be sufficient : #9

  3. RyanCavanaugh commented on Aug 29, 2014

    @RyanCavanaugh
    Member

    What 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.

  4. disshishkov commented on Aug 29, 2014

    @disshishkov
    Author

    Some 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.

  5. RyanCavanaugh commented on Aug 29, 2014

    @RyanCavanaugh
    Member

    This 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.

  6. rjamesnw commented on Sep 2, 2014

    @rjamesnw

    Personally, 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.

  7. kvantetore commented on Oct 25, 2014

    @kvantetore

    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.

  8. wsmckenz commented on Nov 15, 2014

    @wsmckenz

    +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.

  9. yahiko00 commented on Nov 15, 2014

    @yahiko00

    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(...) { ... }
    }
  10. jfrank14 commented on Dec 4, 2014

    @jfrank14

    +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.

  11. chrizy commented on Dec 4, 2014

    @chrizy

    +1 This will really help me with my auto generated classes that I need to extend

  12. fletchsod-developer commented on Dec 4, 2014

    @fletchsod-developer

    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.

  13. jfrank14 commented on Dec 4, 2014

    @jfrank14

    fletchsod-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.

  14. 206 remaining items

  15. aluanhaddad commented on Apr 10, 2017

    @aluanhaddad
    Contributor

    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.

  16. greendimka commented on Apr 10, 2017

    @greendimka

    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!

  17. greendimka commented on Apr 10, 2017

    @greendimka

    In fact the first post in this topic perfectly describes what was requested, therefore this discussion has no future :)

  18. aluanhaddad commented on Apr 11, 2017

    @aluanhaddad
    Contributor

    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.

  19. greendimka commented on Apr 11, 2017

    @greendimka

    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.

  20. cosmoKenney commented on Apr 11, 2017

    @cosmoKenney

    Aluan 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.

  21. spion commented on Apr 11, 2017

    @spion

    Ken 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.

  22. aluanhaddad commented on Apr 11, 2017

    @aluanhaddad
    Contributor

    spion (@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

    Ken Hadden (@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.

  23. cosmoKenney commented on Apr 11, 2017

    @cosmoKenney

    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.

  24. luisca-dev commented on Apr 25, 2017

    @luisca-dev

    +1

  25. aluanhaddad commented on May 3, 2017

    @aluanhaddad
    Contributor

    Ken Hadden (@cosmoKenney)

    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.

    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.

  26. jeeshenlee commented on May 10, 2017

    @jeeshenlee

    +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).

  27. locked and limited conversation to collaborators on May 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions