TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Partial classes

Open disshishkov opened this issue 12 years ago • 224 comments

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

disshishkov avatar Aug 29 '14 08:08 disshishkov

+1

niemyjski avatar Apr 20 '15 21:04 niemyjski

+1 - We also need it for partially generated classes, would be more elegant than inheritance which emits quite a bit of code that's not needed for this purpose.

hdachev avatar Apr 26 '15 09:04 hdachev

+1

DmitryEfimenko avatar Apr 29 '15 08:04 DmitryEfimenko

Ran into this limitation again - please add this feature! :+1:

JoshMcCullough avatar May 21 '15 15:05 JoshMcCullough

+1

laszlojakab avatar May 21 '15 16:05 laszlojakab

+1

roelvanlisdonk avatar Jun 05 '15 08:06 roelvanlisdonk

+1

czlatea avatar Jun 05 '15 09:06 czlatea

+1

muzuiget avatar Jun 16 '15 09:06 muzuiget

People randomly +1 this isn't going to move it on.

The use cases so far are:

  • I have really big classes that span multiple files. @RyanCavanaugh indicated that it was likely a major design issue with code that complex and not sufficient to warrant the complexity of implementation.
  • It like them.
  • An other argument seems to be C# has them.
  • The other argument is around generated code from other languages.

The second and third use case is not compelling and the fourth, I suspect needs some level of expounding how delivering #9 wouldn't meet a very similar requirement and even Decorators (#2249).

kitsonk avatar Jun 16 '15 16:06 kitsonk

It does look like #9 would accomplish the same thing and be even more general (because you could extend classes other than your own). But it might be reasonable to want class content in two files (perhaps because one is hand written and the other generated by a template), and to say that both files define the essential members of the class with equal importance, and not require that one file "extend" the class define in the other.

It's an almost existential difference, but perhaps important to some people in some situations.

jfrank14 avatar Jun 16 '15 16:06 jfrank14

I don't want partial on classes for reasons stated, but on modules it makes sense IMHO.

https://github.com/Microsoft/TypeScript/issues/447

Davidhanson90 avatar Jun 16 '15 17:06 Davidhanson90

Partial classes are extremely useful for specializing use-cases. It is not a violation of any OOP pattern but a way to arrange code document, which provides:

  1. Ability to split the class/type definition in multiple files. For huge code files, it is maintainability++ feature.
  2. Opportunity for compiler/build-system to make use of conditional linkage at compile-time. Analogy MSBuild, concrete use-case in C#: DBML, designer classes and even more fancy stuff like https://github.com/dotnet/corefx/pull/2045/files.
  3. Inherent protection, so two (or more) developers don't run into each other during code check-in. Conflict resolution blows! :)
  4. Let you generate code into the main partial class, then create another partial for custom overloads. Works perfectly!

I believe in separating business logic and data layers into separate classes and I mostly use static methods in these layers. I really don't see how it could be more code and less maintainable.

While extension method is a core language feature influenced by OO patterns and will be processed at compile-time, partial classes deals with document/code-layout to be stitched at pre-compilation stage. Despite the similarities in partial classes, abstract classes and extension methods, there is a clear distinction.

:+1: for bringing the harmless "Partial Classes" to TypeScript.

ghost avatar Jun 21 '15 08:06 ghost

My use case for partial classes is for code generation -- my team needs to be able to auto-generate part of the class in one file and add custom methods and properties by hand in another file. I'm a bit confused why @RyanCavanaugh is "not sure those kind of situations apply in JavaScript/TypeScript", since this use case is unrelated to the language of choice.

Neither decorators nor extension methods solve the code generation problem as elegantly as partial classes, and each has substantial additional complexities.

RandScullard avatar Jun 29 '15 21:06 RandScullard

+1

WangJi avatar Jul 04 '15 22:07 WangJi

+1

kobezzza avatar Jul 05 '15 12:07 kobezzza

+1 for code generation purposes.

junkyjack1 avatar Aug 01 '15 12:08 junkyjack1

In the C# environment, the integration of a transpiler of C# classes to TypeScript classes would remove the need to keep two different definitions of classes between the server and client side as the client side, aka TypeScript class, would be automatically generated and the partial class that this issue is requesting would allow customization when needed while still developing in native TypeScript.

TylerBrinkley avatar Aug 04 '15 20:08 TylerBrinkley

+1 for code generation scenarios

orellabac avatar Aug 07 '15 04:08 orellabac

+1 extremely helpful for code generation

greendimka avatar Aug 10 '15 13:08 greendimka

Why not use inheritance for code generation?

spion avatar Aug 10 '15 14:08 spion