0

When I hover my cursor over a Typescript type in Playground, its type info is displayed correctly. How to get the same type info in .D.TS?

e.g.

type SecondArg<F> = F extends (a: any, b: infer B) => any ? B : never
// Get the type of Array.slice
type F = typeof Array['prototype']['slice']
type A = SecondArg<F> // number | undefined

.D.TS show:

declare type A = SecondArg<F>;

My desired output is:

declare type A = number | undefined;

I tried this, but it does not get the full expanded Typescript type info in .D.TS.

//https://stackoverflow.com/questions/57683303/how-can-i-see-the-full-expanded-contract-of-a-typescript-type
type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
type ExpandedA = Expand<A>;

https://www.typescriptlang.org/play?#code/C4TwDgpgBAyhDGB7AdgEwIICcDmAeAYgHxQC8U+UEAHsBGgM5QAUAhgFxQvIgA0UARhwCWyAGYRMUAEIBKUsS4goAfmlQOyCADcJAWABQAekNQA4hGBRgAC2ihIURKKhZMLEADp6AGyHwIBvbQFGRBTi6YbiAA2gDkYJiIwEngELEAunE+fmnpgakupLAIKBg4BMQGBsbWwMBg9GzG9MAs8ADWiDqYot6IAO4eSAC2hgCOAK4QLUIo9IYArADsAGwAHADMGwAMG4bWAwC08FyHQof0EBCHNteiE97eh9RgXKgQqMcowG7wwIdOQ4sG6pejwTBCMD-IL5BwAUSorzQuAAKsQyCjKDQ6KhGCJxJIAPIqKAAbyg0QA0lARFB2hAQOFCekOISqekoABfdRQTTdADcsOgCKR7wwRRFb1w6EI-KAA

albertkao9
  • 125
  • 8
  • 1
    I don't think there's a way to do this other than to manually replace your definition with the expanded version in the source. [ms/TS#34556](https://github.com/microsoft/TypeScript/issues/34556) is still open, which *might* end up giving you what you want if implemented. – jcalz Oct 14 '22 at 19:36
  • When I hover my cursor over a Typescript type in Playground, its type info is displayed correctly. This suggests that the compiler parses the source codes correctly and generates my desired output. How to get (or generate) the same type info displayed by a cursor hover in Playground? Do the compiler has an option to get (or generate) the same type info? – albertkao9 Oct 15 '22 at 12:32
  • 1
    No, not as part of the language itself. There is a [compiler API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) that you could *possibly* use to get this information, but if you're looking for that you should [edit] the question to ask about that specifically and tag with `typescript-compiler-api` and then hopefully someone who's used that could help you? – jcalz Oct 15 '22 at 19:34
  • I asked the question: https://stackoverflow.com/questions/74133237/get-the-type-info-of-hovering-cursor-in-typescript-playground – albertkao9 Oct 20 '22 at 00:30

0 Answers0