-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Trying to infer a substring in a template literal leads to the type not matching the template #55855
Comments
Your playground link does not match the code from your issue. Your inferred type Any reason why you're inferring the delimiter when you get it provided? When you write |
The code in the issue only illustrate the issue itself.
I'm not sure what you mean by that. Are you saying it will try to match the string starting by the end up until it finds
I was writing a type with |
I think this is likely related to #49839 (it might not be the same though) Just like @MartinJohns said, the problem here is that inferring into type variables with type InferDelimiter<
T extends string,
Delimiter extends string,
> = T extends `${infer Head}${infer Mark}${string}`
? Mark extends Delimiter
? true
: false
: false;
type T1 = InferDelimiter<"foo.bar", ".">; I actually find this behavior quite confusing and I think it would be better if those constraints were taken into account while inferring to template literal types. They are sometimes taken into account when inferring into tuple elements - the constraint infers the algorithm how many fixed elements can be "consumed" from the source. It would make sense to do something similar here. It might be tricky to do it in an efficient way though - checking against the constraint after each consumed character doesn't sound like a good idea. |
Oh ok, thanks for the explanation, I understand now. As for the performance of taking the constraints into account while inferring, because it is inside a template literal, several dramatic optimisations can be made to turn this into a O(1). Edit : Regarding this issue vs #49839 , I'm unsure because this issue shows up with version |
@Andarist If I recall from past discussions, it works this way intentionally for performance reasons, so the template matching algorithm doesnβt ever have to backtrack (any given placeholder either matches on the first try, or it doesnβt match at all). Two directly adjacent placeholders will only ever match a single character at the first one, so you can do stuff like |
I think there might be a way to implement this with simpler lookaheads instead of backtracking - but since the added constraint might be a union (or a generic, or a non-literal...) it definitely makes the whole thing quite tricky π’ I was already thinking about this one a couple of times and each time... I gave up π |
Out of curiosity, what makes it more tricky ? Union means combinatory so it's the same process repeated for each member, for each union, right ? What about generics ? |
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
template literal infer
,literal infer
,infer match
π Version & Regression Information
This changed between versions
4.6.4
and4.7.4
. Doesn't no work in5.2.2
nornightly
.β― Playground Link
https://tsplay.dev/WzDbrN
π» Code
π Actual behavior
T1
is evaluated tofalse
.π Expected behavior
T1
is evaluated totrue
.Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: