Skip to content
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

typeChecker.typeToString() should print most derived alias #19198

Open
jayphelps opened this issue Oct 16, 2017 · 1 comment
Open

typeChecker.typeToString() should print most derived alias #19198

jayphelps opened this issue Oct 16, 2017 · 1 comment
Labels
Bug A bug in TypeScript
Milestone

Comments

@jayphelps
Copy link

jayphelps commented Oct 16, 2017

TypeScript Version: typescript@next

Code

Playground Link

type First<T, R> = (source: T) => R;
type Second<T> = First<T, T>;

declare function example(): Second<string>;

Expected behavior:

Using the compiler API, typeChecker.typeToString(signature.getReturnType()) of example returns the most derived alias, Second<string>

Actual behavior:

typeChecker.typeToString(signature.getReturnType()) returns First<string, string>


IntelliSense in the playground and vscode both show the same thing as typeChecker.typeToString(returnType), so I assume they share the same underlying logic.

I tried examining the type object itself to see if I could reach into private APIs to get it, but I couldn't find it. I also tried various formatting flags, but none of them seemed to give me the desired behavior.

I searched for existing issues on this, #17433 seemed similar but digging into it, I think it's different. It's also possible this behavior was intentional of course, if so, lmk. I'm using the compiler API to generate custom documentation for rxjs and there are sometimes a number of aliases used which simplify much more complex structures and hide internals.

I could try to take a crack at a patch if someone knows where I might find the applicable logic. Seems like it would need to be on the type metadata generation side, since I can't find it in the type returned from signature.getReturnType()?

Cc/ @benlesh

@mhegazy mhegazy added Needs Investigation This issue needs a team member to investigate its status. Bug A bug in TypeScript labels Oct 17, 2017
@mhegazy mhegazy added this to the TypeScript 2.7 milestone Oct 17, 2017
cartant added a commit to cartant/rxjs that referenced this issue Nov 4, 2017
@jayphelps
Copy link
Author

jayphelps commented Nov 7, 2017

This might actually be intended behavior after all, albeit an unfortunate one:

https://www.typescriptlang.org/docs/handbook/advanced-types.html

One difference is that interfaces create a new name that is used everywhere. Type aliases don’t create a new name — for instance, error messages won’t use the alias name. In the code below, hovering over interfaced in an editor will show that it returns an Interface, but will show that aliased returns object literal type.

It's not clear why this would be desired, but as always I could definitely be wrong! 🤡

It also turns out that this section is no longer accurate: microsoft/TypeScript-Handbook#682 It's possibel it's the intended behavior but the naming of the first alias slipped in accidentally from other changes.

jayphelps added a commit to jayphelps/TypeScript-Handbook that referenced this issue Nov 7, 2017
Likely true at one point, but right now aliases actually do create a name that is used for Intellisense and errors. However, if you make aliases of another alias it just uses the first alias name; this has been marked as a bug, but it's possibly intended behavior: microsoft/TypeScript#19198
benlesh pushed a commit to ReactiveX/rxjs that referenced this issue Nov 27, 2017
@mhegazy mhegazy modified the milestones: TypeScript 2.7, TypeScript 2.8 Jan 9, 2018
@mhegazy mhegazy modified the milestones: TypeScript 2.8, TypeScript 2.9 Mar 9, 2018
@mhegazy mhegazy modified the milestones: TypeScript 3.0, Future Jul 2, 2018
@RyanCavanaugh RyanCavanaugh removed Needs Investigation This issue needs a team member to investigate its status. labels Mar 7, 2019
@sandersn sandersn removed their assignment Jan 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants