Skip to content
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

Infer Subtypes from Template Literal Types #43907

Closed
5 tasks done
johnrom opened this issue Apr 30, 2021 · 6 comments
Closed
5 tasks done

Infer Subtypes from Template Literal Types #43907

johnrom opened this issue Apr 30, 2021 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@johnrom
Copy link

johnrom commented Apr 30, 2021

Suggestion

πŸ” Search Terms

Inference
Template Literal Types
Subtypes

βœ… Viability Checklist

My suggestion meets these guidelines:

  • 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 feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

I'd like to be able to infer a subtype from a template literal type. For example:

const canExtend: `myString.${number}` extends `${infer Key}.${number}` ? true : false = true;

πŸ“ƒ Motivating Example

This will allow cross-matching between Values and Paths when traversing an object with dot notation.

πŸ’» Use Cases

See line 35, in this Playground

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Apr 30, 2021
@johnrom
Copy link
Author

johnrom commented Apr 30, 2021

I was able to kind of work around this limitation by using the string value "0" instead of ${number}, and then converting that to ${number} later down the road.

It seems to randomly cause "Type instantiation is excessively deep and possibly infinite". I'm open to other workarounds that might not cause so much recursion if anyone has any! You can see changes here, I marked the comments with CHANGE:

Playground

@johnrom
Copy link
Author

johnrom commented Apr 30, 2021

For a real world example, here is Formik with the above integration, with strongly typed Fields which enforce valid name, value, child component, or parse / format function. There are a lot of variants on the core Field component, so testing it is unfortunately very verbose.

https://codesandbox.io/s/serene-napier-4wjxs?file=/src/App.tsx

@DanielRosenwasser
Copy link
Member

This seems to work in nightlies, right?

@DanielRosenwasser DanielRosenwasser added Fixed A PR has been merged for this issue and removed Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels May 1, 2021
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented May 1, 2021

Check it out using a nightly release of TypeScript

@DanielRosenwasser
Copy link
Member

(Fixed by #43361, duplicate of #43243)

@DanielRosenwasser DanielRosenwasser added Duplicate An existing issue was already created and removed Fixed A PR has been merged for this issue labels May 1, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a '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
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants