Skip to content

Infer keyword in typearg constraint #59158

Not planned
Not planned
@MichalMarsalek

Description

@MichalMarsalek

πŸ” Search Terms

"implicit conditional type", "conditional type without extends", "infer outside conditional type", "infer in generic constraint", "conditional type sugar"

βœ… Viability Checklist

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
    This wouldn't change the runtime behavior of existing JavaScript code
    This could be implemented without emitting different JS based on the types of the expressions
    This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
    This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
    This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

⭐ Suggestion

Currently it's not possible touse the infer keyword outside of the "extends type" in a conditional type. I'd like to propose allowing it in a type arg constraint position.

πŸ“ƒ Motivating Example

This seems like a perfectly valid thing to do

type FirstWord<T extends `${infer Result} ${string}`> = Result

but it is not alowed.

There is a workaround using a conditional type

type FirstWord<T extends `${string} ${string}`> = T extends `${infer Result} ${string}` ? Result : never;

but it feels weird having to type the same condition twice. Also, the false branch is provably never hit, which makes it feel more weird having to use the conditional type.

It would probably be easiest to implement if the proposed syntax was simple treated as a syntactic sugar for the conditional type above.

πŸ’» Use Cases

  1. What do you want to use this for?
    Irrefutable pattern matching in type space.
  2. What shortcomings exist with current approaches?
    Having to duplicate the same condition twice, worse readability.
  3. What workarounds are you using in the meantime?
    A conditional type
type FirstWord<T extends `${string} ${string}`> = T extends `${infer Result} ${string}` ? Result : never;

Activity

MartinJohns

MartinJohns commented on Jul 6, 2024

@MartinJohns
Contributor

Duplicate of #39736. Used search terms: infer constraint in:title

typescript-bot

typescript-bot commented on Jul 12, 2024

@typescript-bot
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @DanielRosenwasser@MartinJohns@MichalMarsalek@typescript-bot

        Issue actions

          Infer keyword in typearg constraint Β· Issue #59158 Β· microsoft/TypeScript