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!"; }
}
Do you have other solutions?
I would love to see this added to TS too
+1 for code generation purposes (DTOs from a WCF service reference with additional partial class definitions)
+1 for code generation purposes. My use case is: I have React components whose render() functions are generated externally via react-templates.
With partial classes I could type-check such functions versus the main class (the Component). Without partial classes, the only thing I can do is binding the function to the main class and hope for the best.
With next TS 2.0, I was thinking that some code-generation cases might be covered with the this type feature.
For example in the case I described previously, instead of
partial class MyComponent extends React.Component<any,any> {
render() {
...
}
}
I can write
function render<this extends MyComponent>()
...
}
MyComponent.prototype.render = render;
+1 As a developer I want partial classes so that multiple templates can generate/modify the same class spread out over multiple files.
My comments were specifically in regard to partial classes, not to mixins. The two are fundamentally different. Partial classes are about breaking up physical code, mixins are about breaking up logical behavior into reusable traits enriching other objects at both the type and the value level. I do not think think partial classes are a good idea for TypeScript. Mixins, on the other hand are a good idea, providing increased expressiveness and meshing extremely well with both TypeScript's type system and JavaScript idioms as pointed out by @aleksey-bykov
@wendellm can you think of a clean way to do this given that ES Modules are physical. Partial classes work well and make a lot of sense in a language like C# where modules are logical not physical. From the point of view of the CLR, namespaces don't exist, it's just that class names can have dots in them (source an interview with @ahejlsberg)
+1 I need partial class!
can we extension interface? interface can also have some implemented functions, just like Swift:
interface Rect {
x: number
y: number
}
extension Rect {
area() => this.x * this.y
}
Swift version:
protocol Rect {
var x: Float {get}
var y: Float {get}
}
extension Rect {
func area() -> Float {
return self.x * self.y
}
}
+1
Code generation and partial classes go hand in hand.
Inheritance is a poor solution to what is essentially a glorified #include directive.
Take Angular 2 for example. It will not read metadata from the parent class, making extension-by-inheritance impractical.
@tsvetomir that's an Angular issue not a TypeScript issue.
Inheritance is a poor solution to what is essentially a glorified #include directive
Yes inheritance is a poor solution, but the problem is using classes in the first place. They have their place but it is very limited. JavaScript classes are pretty weak and inexpressive compared to classes in other languages.
Typescript is not an"Angular" language... Never was
On Jul 23, 2016, at 9:46 PM, Aluan Haddad <[email protected]mailto:[email protected]> wrote:
@tsvetomirhttps://github.com/tsvetomir that's an Angular issue not a TypeScript issue.
Inheritance is a poor solution to what is essentially a glorified #include directive
Yes inheritance is a poor solution, but the problem is using classes in the first place. They have their place but it is very limited. JavaScript classes are pretty weak and inexpressive compared to classes in other languages.
You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/Microsoft/TypeScript/issues/563#issuecomment-234753589, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJPCIh7n2_0dt00kw-XJv7tc9LB0tPsIks5qYtH4gaJpZM4CcixK.
I don't see the issue as Angular specific in any way. It is a particular case in which partial classes may have helped.
The request originates from past experience with C# and the lack of familiar features. The comparison is inevitable as TypeScript makes no effort to distance itself from .NET. Some form of feature parity is expected by its users.
The request originates from past experience with C# and the lack of familiar features. The comparison is inevitable as TypeScript makes no effort to distance itself from .NET. Some form of feature parity is expected by its users.
@tsvetomir as a long time .NET developer, and as a passionate C# programmer, I fundamentally disagree.
The article you cited is not authoritative, and does not reflect any official material from the TypeScript team.
Furthermore, it directly contradicts the TypeScript design philosophy as stated by @ahejlsberg in numerous talks, interviews, and posts.
Additionally, the cited article's viewpoint reflects a fundamental misunderstanding as to the nature of TypeScript. Ironically, the commonalities that C# does have with TypeScript are actually the same ones it has with JavaScript. These commonalities are not class-based programming, but rather constructs such as closures and higher order functions.
C# or not, we need partial classes which will help TypeScript getting closer to Javascript's prototype features.
@yahiko00 TypeScript already has all of JavaScript's prototype features. It is a superset.
There are several problems with the notion of partial classes in TypeScript, including
-
Unlike C#, TypeScript (JavaScript) class definitions are imperative not declarative. They appear declarative, but they are not. This means they depend upon deterministic execution order.
-
The ECMAScript module system is based on importing physical not logical code units. For example, lets say I have
app/my-class-part-1.ts
export partial class MyClass { firstName = "John"; lastName = "Smith"; }and app/my-class-part-2.ts
export partial class MyClass { fullName = this.firstName + ' ' + this.lastName; }How can I import this class? Since I cannot import from either module, let us imagine a hypothetical abstraction in TypeScript that allows me to write app/main.ts
import { MyClass } from './my-class';What would this mean? Even if the TypeScript compiler could infer that app/my-class-part-1.ts has to be loaded before app/my-class-part-2.ts, it cannot make the importing of the individual parts invalid nor can it ensure they are imported in the correct order by an asynchronous module loader such as RequireJS or what will eventually be implemented in browsers.
The whole notion of partial classes is fundamentally at odds with the ECMAScript module system.
Update: It would need to perform arbitrarily complex dependency inference and emit some very odd JavaScript.
TypeScript already has all of JavaScript's prototype features. It is a superset.
I should have expressed myself better. Of course we all know TypeScript is a superset of JavaScript. But I wanted to say that partial classes would help TypeScript classes to get closer to JS prototypes. For the moment, we can just have "static" and non extendable classes, whereas, with a prototype approach, we can extend class-like functions.
Importing partial classes would lead to extends the current ES6 syntax I agree. We could imagine something like this:
import { MyClass } from ['app/my-class-part-1', 'app/my-class-part-2'];
TypeScript classes are based on ECMAScript classes and thusly suffer from the latter's lack of expressiveness. Consider using functions, modules, "namespaces", or plain objects to accomplish what you need.