-
Notifications
You must be signed in to change notification settings - Fork 12.8k
How to print anonymous recursive types #463
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
Comments
Related to #402? |
Yes, it is related |
I don't think that should be a bug. Consider: declare function f(): { foo: typeof f; }
var g: () => { foo: typeof g; } We currently permit both of those and correctly generate recursive types. The old compiler is oddly inconsistent in these cases, making an We should fix the type printer to handle the recursion. |
I think recursive types are nice, but typeToString does not print them correctly. Right now if you try to print the type of a in my initial example: var a: { foo: typeof a; }; You get var a: { foo: typeof a; };
var a: { foo: any; }; You will get an error that the types are not identical, yet the resulting error will tell you that both types are |
Also, if we keep the type recursive and do not change it to var a = { foo: a };
var b = [b];
var c = () => c; |
And then we'd get into inconsistencies when we have something of the following form: var a = foo(a); for some function foo. var a: typeof a; |
Yes, I was talking recursive types in type annotations. This is something we already support and we shouldn't undo that simply because we can't produce an accurate string representation of those types. I think the current solution of outputting We might consider outputting { foo: { foo: { foo: ... } } } But that obviously is more work and might cause us to generate an invalid .d.ts unless we also include logic to produce errors in such cases. |
I don't think we should deemphasize the importance of printing the type correctly. It is a big part of the user experience of the language. I do think that we should ensure we have a reasonable way to print a type before trying to support it in the type system. One thing the user should never get is a message like the following: Subsequent variable declarations must have the same type. Variable 'a' must be of type '{ foo: any; }', but here has type '{ foo: any; }'. This is the result of trying to compile the example I provided above: var a: { foo: typeof a; };
var a: { foo: any; }; |
I'm going to reopen this bug and change the title. This bug is still tracking the issue for actually printing the types correctly. There is good material here we can use for that. Also going to mention design bug #517, since it's very relevant here. |
A fix here should handle the case outlined in #475. |
To the best of my understanding of the issue at hand - this is already fixed and the issue could be closed cc @RyanCavanaugh |
Wow, throwback. Indeed. Thanks @Andarist ! |
a
has a recursive type. If we try to print this type, it prints{ foo: any; }
, but it does not behave as such in type comparisons. So we are lying when we print it.When fixing this, it's important to consider both displaying a type to the user, and generating .d.ts files.
The text was updated successfully, but these errors were encountered: