Emit classes instead of vars
Now that we can extend both class instance type and static type, can we now directly emit classes? Is there any remaining blocking problems?
The ability to emit classes would help implement my proposed solution to the WebGL declarations' use of empty interfaces: https://github.com/Microsoft/TypeScript/issues/5855#issuecomment-293117701
I will submit a PR if someone gives Accepting PR tag.
On the other hand, emitting classes makes it much harder to extend built-in types with optional extensions, since class declarations cannot be merged together, but interfaces can.
class declarations cannot be merged together, but interfaces can.
This works:
declare class Merge {
x: string;
}
interface Merge {
y: string;
}
new Merge().y;
It can't extend static properties, but it never worked anyway.
@saschanaz Yeah I'm trying to fix that right now actually, because ran into a problem with polyfilling static methods :) https://github.com/microsoft/TSJS-lib-generator/pull/812
To be clear - I came across this issue because I'd also be happy if there was a way to reduce all that clutter and just use class, but polyfilling is quite important for Web APIs, and not being able to do it seems like a dealbreaker.
Hopefully this can be somehow fixed on TypeScript side in the future by allowing merging classes with classes, or adding static methods to interfaces or something similar.
There is a relevant issue on TS: https://github.com/microsoft/TypeScript/issues/2957
Actually... I've just realised that something like this might work for extending statics:
declare class Merge {
x: string;
static X: string;
}
interface Merge {
y: string;
}
declare namespace Merge {
const Y: string;
}
new Merge().x;
new Merge().y;
Merge.X;
Merge.Y;
So if we implement this, then #812 would be unnecessary.
Either option is fine by me and better than current status quo that doesn't allow any extensions, but this one would probably be even cleaner.
FWIW - just so that our work doesn't conflict - I'm giving this a try.
Update: I have a working implementation of this, but waiting for #813 to be merged first.
I tried this in https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/858, but it failed because TypeScript doesn’t support var‑like classes (see https://github.com/microsoft/TypeScript/issues/39504#issuecomment-655672993)