Skip to content

Infer keyword in typearg constraint #59158

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

Closed
6 tasks done
MichalMarsalek opened this issue Jul 6, 2024 Β· 2 comments
Closed
6 tasks done

Infer keyword in typearg constraint #59158

MichalMarsalek opened this issue Jul 6, 2024 Β· 2 comments
Labels
Duplicate An existing issue was already created

Comments

@MichalMarsalek
Copy link
Contributor

πŸ” Search Terms

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

βœ… Viability Checklist

⭐ 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;
@MartinJohns
Copy link
Contributor

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

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Jul 9, 2024
@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants