Skip to content

user friendly inferΒ #62478

@AcsedyFN

Description

@AcsedyFN

πŸ” Search Terms

infer too complicated, infer logic mismatch, infer improvement

βœ… Viability Checklist

⭐ Suggestion

In an expression such as type Something<T> = T extends Promise<infer U> ? U : never; the occurence of "infer U" will receive the type within the pattern it resides and syntactically suggests that U is the type that is received within that layer (thinking about layer depth as in PromiseIsLayer1<infer UIsLayer2OrTheInnermostLayer>). However, within the conditional it becomes clear that U now refers to the pattern as a whole, checking not only the received type within the (in this case) generic, but also the structure surrounding it.

Therefore I suggest the infer keyword to be used differently and a new keyword to be added (any solution that is a readable and self explanatory syntax would be sufficient)

πŸ“ƒ Motivating Example

type Something<T> = T extends pattern Promise<infer> U ? U : never;
The keyword "pattern" would ensure the intuitive understanding of the following to be taken as a whole and assign it to the temporary type variable U, making it explicitly a type consisting of that pattern. The infer keyword only holds the type information and places it in the resolved form of U, in that case we could assume Something<Promise<string>> being Something<U>. I see no reason not to omit the variable next to infer, which could receive a variable to store the type information in under the hood.

πŸ’» Use Cases

  1. What do you want to use this for?
    A better developer experience, that is more intuitive and fits the logic the underlying implementation follows.

  2. What shortcomings exist with current approaches?
    It is not intuitive and exhibits logical flaws that have to be overcome to gain the understanding that could be easily there.

  3. What workarounds are you using in the meantime?
    Understanding the concept using more time than necessary. Grasping it isn't the problem, it is the average amount of time spent on it, that holds developers back.

Metadata

Metadata

Assignees

No one assigned

    Labels

    UnactionableThere isn't something we can do with this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions