Skip to content

Infer Subtypes from Template Literal TypesΒ #43907

Closed

Description

@johnrom

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

Activity

  1. johnrom commented on Apr 30, 2021

    @johnrom
    Author

    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

  2. johnrom commented on Apr 30, 2021

    @johnrom
    Author

    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

  3. DanielRosenwasser commented on May 1, 2021

    @DanielRosenwasser
    Member

    This seems to work in nightlies, right?

  4. added
    FixedA PR has been merged for this issue
    and removed
    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this feature
    SuggestionAn idea for TypeScript
    on May 1, 2021
  5. DanielRosenwasser commented on May 1, 2021

    @DanielRosenwasser
    Member
  6. DanielRosenwasser commented on May 1, 2021

    @DanielRosenwasser
    Member

    (Fixed by #43361, duplicate of #43243)

  7. added
    DuplicateAn existing issue was already created
    and removed
    FixedA PR has been merged for this issue
    on May 1, 2021
  8. typescript-bot commented on May 3, 2021

    @typescript-bot
    Contributor

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

  9. locked as resolved and limited conversation to collaborators on Oct 21, 2025
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

      Issue actions