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!"; }
}
+1
+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.
+1
Ran into this limitation again - please add this feature! :+1:
+1
+1
+1
+1
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).
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.
I don't want partial on classes for reasons stated, but on modules it makes sense IMHO.
https://github.com/Microsoft/TypeScript/issues/447
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:
- Ability to split the class/type definition in multiple files. For huge code files, it is maintainability++ feature.
- 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.
- Inherent protection, so two (or more) developers don't run into each other during code check-in. Conflict resolution blows! :)
- 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.
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.
+1
+1
+1 for code generation purposes.
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.
+1 for code generation scenarios
+1 extremely helpful for code generation
Why not use inheritance for code generation?