-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion: allow instantiation of type aliases #2559
Comments
We'd need some new type system machinery here. Consider something like this: class Foo<T, U> {
constructor(public x: T, public y: U) { }
}
var f = Foo; The type of {
new<T, U>(x: T, y: U): { x: T; y: U; };
} Note that this type itself is not generic, so the expression This is to say nothing of class-like things that have been modeled in .d.ts files as interface/var pairs instead of classes. What should this code do? interface Foo<T, U> {
x: T;
y: U;
}
var Foo: {
new<S>(n: string): Foo<number, S>;
new<S, T>(n: number, m: string, z: any): Foo<T, U>;
new<S, T, U>(n: number): Foo<T, U>;
}
import q = Foo<number, number>; // Is this even legal? What does it mean?
var j = new q('hello'); // Valid? |
Thanks, it's good to know. Clearly there is no straight way of doing it by the means TS currently has.
import q = Foo<number, number>; // Legal. It means that q is { new (n: number, m: string, z: any): Foo<number, number>; }
var j = new q('hello'); // Invalid since q is { new (n: number, m: string, z: any): Foo<number, number>; } |
Does #1616 cover this? |
@danquirk it does if, besides being capable of type parameters, you add to it being capable of aliasing a class so that instantiating of such alias is possible |
Yeah that's what I thought. I'll modify the title here to specifically reflect the instantiation aspect and we can use that issue to track the type parameters part since it already has a bunch of discussion and upvotes. |
Is there any chance the proposal for this can be completed? This feature is really important in my honest opinion. |
there is a workaround for classes |
@Aleksey-Bykov Is really weird object destructuring doesn't work with import aliases, meaning this works: import {fabric} from 'fabric';
import Canvas = fabric.Canvas; but this doesnt import {fabric} from 'fabric';
import {Canvas} = fabric; So you need to be too verbose if multiple aliases are needed because you need one line per alias. |
Here's a proposal that should address this quite nicely.
@RyanCavanaugh I'm guessing it didn't exist back when this written, but the machinery and semantics around subclassing do exactly what we want here. In particular:
It would mean exactly what
I don't imagine that being too controversial, since it mirrors existing behavior. I think the only remaining question would be syntax. Rather than extending |
I ran into this and just realized I could just replace
with
|
adding a use case: type definition files. When I want to declare a class that extends an imported class, I currently have to do this: // Aclass is `class` but A is `type`
type A = import('./a-class').AClass
// @ts-ignore -- complains that A methods aren't implemented
declare class AC implements A {}
declare class B extends AC {
} This seems to work but is ugly |
Currently a type aliased class cannot be instantiated. Also there is no way to alias a type with type parameters. Please consider making type aliases as capable as the types that they represent, basically capturing the type expression under a different name.
The text was updated successfully, but these errors were encountered: