-
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
Allow const variables with new signatures to be types #18942
Comments
So, I was going to say something like this: You're asking for TypeScript to implicitly define a type with the same name as a value you created, the way it does with `class`. In the absence of that, you can explicitly define that type yourself: But unfortunately, the This erasure feels like an actual bug or design limitation, but my search-fu wasn't strong enough to identify if there's a reported issue about it. Is it related to #13807? Is this just a casualty of not having higher kinded generics (#9366)? |
Yes, strange that it didn't work with |
The issue here is you have no way in the language to refer to the return type of a constructor function. in a class, the class name addresses that, but not for a non-class constructor. #6606 tracks addressing this. |
@mhegazy I don't think I understand your answer completely. #6606 isn't it different? It will allow you to write
|
@tinganho your line of reasoning definitely makes sense, but I think that there would be a few issues with changing the current behavior. It would be a breaking change to existing code that explicitly declared a type such as const User = defineModel();
type User = typeof User['prototype'];
export default User; There are also scoping issues which could interact oddly with declaration merging. |
@mhegazy Which issue is this a duplicate of? #6606? I don't see how extending Maybe to clear things up we should keep this issue for the suggestion to get implicitly defined type names for class constructor constants, and I should create a new one for the problematic-generic-erasure issue that prevents |
In the OP, the instance type that @tinganho wants to use is the type of the expression
As you noted,
As i noted earlier, values and types are not the same thing. this is a fundamental concept of the TypeScript type system that allows declaration merging for the same declaration name in multiple spaces. Changing what a value vs. type means is first not correct and second would be a breaking change. today |
@mhegazy Thanks for your reply.
I understand this and agree that implicitly assigning types to
I guess I still don't understand why |
@mhegazy I proposed in my OP to use the name of the variable |
it is instantiated with
Sorry for the confusing labeling. the suggestion for
This solution, as well as something like |
I think this was a bit fast declined. I think this is quite a usual pattern to define a factory function that defines an object in JS. You not only want to use the value of the object everywhere, but also the type. Letting people defining both the value and the type is quite frustrating. Class declarations that extends the return type of a function is a solution, though it looks very hackish. At the end, a variable declaration that gets assigned a class declaration is nearly identical to a normal class declaration. And IMO, more idiomatic JS. |
Problems with creating a type name include:
Consider something like this type Blah = { };
namespace X {
var y: Blah = 10;
const Blah = someFunc(y);
} The meaning of Overall #6606 is the best way to address this scenario |
I want to make use of inference when defining a class. Thus, I create a function returning a class instead of defining a class the normal way:
Though, I think the line
class User extends Model() {};
is a bit non-idiomatic-js and ugly. It would be great if I could do this instead:At the end, this is working in JS, but TS prevents me.
The text was updated successfully, but these errors were encountered: