question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A way to expand mapped types

See original GitHub issue

I want to be able to expand mapped type in intellisense

Currently, mapped types is displayed directly with the mapper and original type, which is unhelpful as hell

2018-11-13 12 33 16

With the ts 2.8 condition type, you can write more powerful declaration to map almost everything to correct type for you, but the ide simply show it is mapped, you don’t know what is it actually mapped to.

for example, in the above case, I have a mapper looks like


type Mapper<T> = {
    [K in keyof T]: 
        T[K] extends {type: SQL_ENUM<infer U>}? U:
        T[K] extends {type: SQL_ENUM<infer U>, allowNull: true}? U | undefined:
        
        T[K] extends {type: typeof Sequelize.DATE, allowNull: true}? Date | undefined:
        T[K] extends {type: typeof Sequelize.DATE}? Date:

        T[K] extends {type: typeof Sequelize.INTEGER, allowNull: true}? number | undefined:
        T[K] extends {type: typeof Sequelize.INTEGER}? number:

        // stop here, fon't let things goes too wrong
        T[K] extends {type: typeof Sequelize.ENUM}? never:

        T[K] extends {type: typeof Sequelize.STRING, allowNull: true}? string | undefined:
        T[K] extends {type: typeof Sequelize.STRING}? string:

        T[K] extends {type: typeof Sequelize.TEXT, allowNull: true}? string | undefined:
        T[K] extends {type: typeof Sequelize.TEXT}? string:

        T[K] extends {type: typeof Sequelize.BOOLEAN, allowNull: true}? boolean | undefined:
        T[K] extends {type: typeof Sequelize.BOOLEAN}? boolean:

        any
}

that will transform the decalration to a simple

interface session {
    token: string,
    userId: string,
    ip: string
}

But the ide won’t tell you anything, which is quite annoying

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:35
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
jantimoncommented, Nov 6, 2019

This might look like the expand feature as described in #34944?

Typescript

7reactions
weswighamcommented, Nov 13, 2018

@mjbvz could we perhaps expose consider exposing clickable “expando” ranges in quick info/signature help that fire a request to a new language server endpoint with a token associated with the range for expanded content for the range (and that expanded content may itself have more things which need expansion)? Off the top of my head, I can think of four places where this’d be useful:

  1. Generics, aliases, and type queries. Click the a type reference to replace it with the structural decomposition (or conditional, union, whatever the underlying structure is) of the reference.

  2. The any or ... we print for reverse mapped types. These types are limited in how much we print, as they have each property inferred on demand (as they each need to be mapped backwards through a mapped type). This is what happens when, for example, we infer the T in a Readonly<T> - we need to undo the readonlyness on whatever we find for T and we do that in a deferred fashion (since otherwise a circular type reference in the T would blow up, as we have no good name for our unmapped T to reference with). Being able to expand these one property at a time in the editor as needed would be great.

  3. The any or ... we print on encountering circular referential local types. This happens when, for example, you have two mutually recursive functions that return one another. We actually type check this strongly, but both will just print as () => () => any or () => () => ... (as of yesterday) today, since we have no reference that we know will stick around with which to indicate the circularity.

  4. The ... we print in the output when the type is truncated for being too long. Sometimes, even when a type is truncated, the part you care about isn’t in the beginning bit and you just wanna see more. Turning on noErrorTruncation works, but then you might always get huge types (and commensurately longer LS response times) - being able to expand little bits on demand could be a boon here.

cc @DanielRosenwasser and @RyanCavanaugh since I think we’ve mentioned this before.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mastering TypeScript mapped types - LogRocket Blog
In this post, we'll cover mapped types in TypeScript, a real-world example of them, and utility types including Partial, Readonly, and Pick.
Read more >
Cannot extend mapped types · Issue #12986 - GitHub
What if what I want is not to "inherit" from the mapped type, but simply use it as a "model" or "constraint" for...
Read more >
Mastering TypeScript's Mapped Types | by Jose Granja
What are Typescript mapped types? They are a way to avoid defining interfaces over and over. You can base a type on another...
Read more >
typescript - Creating an exclusive union from a mapped type
I can create the argument types for said function by using mapped types to create a union of the types of the original...
Read more >
Perfecting Mapped Types in TypeScript - Upmostly
If you wanted to implement this manually, you'd have to rewrite your type and copy every property, changing each one to be optional....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found