TypeScript-DOM-lib-generator icon indicating copy to clipboard operation
TypeScript-DOM-lib-generator copied to clipboard

Emit classes instead of vars

Open saschanaz opened this issue 8 years ago • 11 comments

Now that we can extend both class instance type and static type, can we now directly emit classes? Is there any remaining blocking problems?

saschanaz avatar Mar 28 '17 07:03 saschanaz

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

sufianrhazi avatar Apr 11 '17 01:04 sufianrhazi

I will submit a PR if someone gives Accepting PR tag.

saschanaz avatar Apr 13 '17 17:04 saschanaz

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.

RReverser avatar Dec 17 '19 14:12 RReverser

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 avatar Dec 17 '19 14:12 saschanaz

@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

RReverser avatar Dec 17 '19 14:12 RReverser

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.

RReverser avatar Dec 17 '19 15:12 RReverser

There is a relevant issue on TS: https://github.com/microsoft/TypeScript/issues/2957

saschanaz avatar Dec 17 '19 15:12 saschanaz

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.

RReverser avatar Dec 17 '19 16:12 RReverser

FWIW - just so that our work doesn't conflict - I'm giving this a try.

RReverser avatar Dec 17 '19 20:12 RReverser

Update: I have a working implementation of this, but waiting for #813 to be merged first.

RReverser avatar Dec 18 '19 19:12 RReverser

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)

ExE-Boss avatar Dec 09 '24 17:12 ExE-Boss