-
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
Ability to access the type of a class outside the lexical scope it's defined in #6179
Comments
Could #4233 help here? function foo() {
return class Anonymous {
/*...*/
}
}
type FooType = typeof foo(); // expression is not evaluated, just used to compute a type |
Oh, nice. Didn't see this interesting proposal about |
Should we close this in favor of #4233, or is there a need for a plausible proposal? |
A closer look at #4233 made me think that for classes, that syntax wouldn't be elegant at all. Still much better than nothing though. In @yortus 's example above, the type that is assigned to type FooConstructor = typeof foo();
type FooInstance = typeof new (foo()); And such code would be needed for every class we want to make global. Whereas the syntax I proposed does the same with one modifier keyword. It'll make instance types global, and the constructor types of 'globalized' classes should be accessible via Effectively this modifier should work as if it created an ambient class declaration in the global scope: function foo() {
public class Bar {
// ...
}
} is supposed to mean the same thing as function foo() {
class Bar {
// ...
}
}
declare class Bar {
// ...
} It's simple and readable. So, I'm against closing this issue. |
accessing a name outside its scope is not a possible. there are scoping and shadowing rules in effect already, and i do not see how you can make something like this work for the general case. |
In JS, you can create a global variable at any point just by omitting var/let/const (well, we can't be sure it'll be really global) or by assigning to a property of the global object. So it's natural to expect similar scoping possibilities from types. |
I do not think this specifically was a good design decision for JS :) |
If it comes about 'really global' variables, of course, I agree with you. But also we can do this: var a;
function f() {
a = ...
} I wish we could do something like that with types. It'd be logical and symmetrical. |
It's common and idiomatic in Angular 1.x apps to have code like this:
Defining classes this way (using a factory function with dependencies injected to it as its parameters) is often the right thing to do from the point of view of Angular's approach to dependency injection because:
service1
,service2
).MyService.id
) that, ideally, shouldn't be shared between different instances of the container.However, this code can't be ported easily to TypeScript as the type
MyService
isn't visible for the rest of the app. We have to define also an interface for it and remember to change the interface every time the class is changed. I wish there were a way to make such types visible globally. Can't think of a good keyword for this. Probably,public
orglobal
will do.The text was updated successfully, but these errors were encountered: