-
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
Type alias name (union) is not preserved when using index access/lookup types #32287
Comments
Duplicate of #31940. |
This issue seems more specific than #31940, so not sure it's a duplicate, but the general idea is the same. |
The fact that any type alias at all shows up is due to trickery and the limitations of that trickery are apparent in these examples. The trick is that if the very first time we make a type it's because it's initializing a type alias, then future displays of that type use the alias name. This is a desirable heuristic because you don't want some rando .d.ts file to say type AcrobaticBackCompatKey = string | number | boolean; and then have your function function fn(stringish: string | number | boolean) { } display back as fn(stringish: AcrobaticBackCompatKey) which you didn't write. In this example, it seems the type "Why not just make type aliases be a named pointer to the underlying type?" Nothing's impossible, but this has some performance impacts that aren't immediately obvious. For example type SN = string | number;
type NB = number | boolean;
declare let q: SN | boolean;
declare let v: string | NB;
q = v;
v = q; In this example, the assignments can be detected to be correct almost "for free" because their types are literally the same object - there's no way to tell So it's a trade-off and we'd have to figure out ways to make it not as expensive as it might be, or someone can maybe propose tweaks to our trickery that would make things more palatable while still not messing up existing type display too badly. |
Thank you for the explanation. Unfortunately I don't have any ideas how you can resolve this. |
I've never dug into the compiler source, but I have an idea: Keep one type object around for typechecking, and another separate object for diagnostic reporting (only if different than the first). So for Ryan's example, the AST for
Of course I'm just this random guy and the devil is always in the details. |
@OliverJAsh this has changed between |
TypeScript Version: 3.5.2
Search Terms: indexed access lookup types type alias naming
Code
Given:
If I hover over
MyObject
, I see{ value: MyUnion; }
.However, given:
If I hover over
MyObject
, I see{ value: 1 | 2 | 3; }
.I would expect the same behaviour as we see in my first example (
{ value: MyUnion; }
). The name of the type alias,MyUnion
, should be preserved and its usage/reference inside ofMyObject
should be shown.To give some context on my real world use case for this: I am using Unionize to create tagged unions (to avoid boilerplate for constructors, matchers, and type predicates). However, because of the issue described above, the types become very difficult to inspect/read: pelotom/unionize#60.
Under the hood, Unionize does something like this:
If we hover over
MyObject
here, we see:… when we really want to see
{ value: MyUnion }
.As I demonstrated in pelotom/unionize#60, this scales really badly, e.g. when nesting unions.
The text was updated successfully, but these errors were encountered: