Repository navigation
type inference issue with dynamic template literals Β #58402
Copy link
Copy link
Open
Labels
Domain: check: Type InferenceRelated to type inference performed during signature resolution or `infer` type resolutionRelated to type inference performed during signature resolution or `infer` type resolutionHelp WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
Description
Activity
- addedHelp WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
on May 2, 2024 Andarist commented
on May 3, 2024 ContributorMore actionsIn a sense, this could be seen as a duplicate of #13948 . Like I mentioned here the computed properties like this are always widened - without considering the contextual property keys.
A somewhat related thing to this is that those assignments work because index signatures turn off common property checking:
const obj: { [key: `hello.${string}`]: string } = {} as { [key: string]: string; };
This is OK but it would - IMHO - make sense for this to participate in the common property check. You can see a different issue related to this: #55709
Not sure if it's related to this but I'm facing a similar problem:
type Original = `/AnyString/${number}`; type Match = Original extends `${infer Head}/${number}${infer Tail}` ? { a: Head, b: Tail } : never;
Here
Matchisneverbecause apparently it matches/AnyStringas/${number}and then fails becauseAnyStringis not anumber.- addedDomain: check: Type InferenceRelated to type inference performed during signature resolution or `infer` type resolutionRelated to type inference performed during signature resolution or `infer` type resolution
on Oct 16, 2025
Metadata
Metadata
Assignees
Labels
Domain: check: Type InferenceRelated to type inference performed during signature resolution or `infer` type resolutionRelated to type inference performed during signature resolution or `infer` type resolutionHelp WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
π Search Terms
"template literals and type inference", "type inference with dynamic template literals"
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.4.5#code/C4TwDgpgBAgmCWUC8UDeAoKUDaBrCIAXFAAYAWEANpQPYB0AJKgM7ABO8AdgOYC+JAXWIAKACYBDYOOKooncQFsIxVhx5ReASmQA+KADca8Uel7p0lCMCgBXZhDYBJUSvZduyKAHJRIeQvgAYyhVdy9zAHoIqABRAA9FMEsARmIAERoIZk4vawB3GjZcOihQSCgAM0LSCBIoQPEc6wAjaC4KhzYIE0CaTlZSrOBU2ARPDCxscipaRlQ7B2d+IShoJD0JrHq+5hpLOlpuYQg6fwhNTA0AGlNI6PjEywAmYgB1QtxmdF7+62Ahl6jRAoTZTCjUeisSRBAD6oR4gmIaw2lywP12+0Ox1OinOl14NzM6CisQSCiSEAAzG8Pl90dY+p4ADwAaVWcX+nFEzCg+BANAqQJ0wkufOILJuWHEgWA8D6IgkUmIcHg2BZAm06wMRlEN01KKwJN2SighyCt3QfWE0whcwWTlE-Cuq10aEu6L2JyxJzOFy0QA
π» Code
π Actual behavior
Example 1doesn't work, whileExample 2andExample 3works properly. The type forecan't be inferredπ Expected behavior
To infer type of
ein theExample 1.Additional information about the issue
No response