#Why the results types are not the same?

18 messages ยท Page 1 of 1 (latest)

grizzled charmBOT
#
stavalfi#0

Preview:ts // i expected the following to return "aaa" but it return false. Why? type x1 = "aaa " extends `${infer Str}${infer _ extends " "}` ? Str : false; // false // Same expression with the expected result. type x3 = "aaa " extends `${infer Str} ` ? Str : false; // "aaa" ...

grizzled charmBOT
#
type x4 = "aaa aaa bbb" extends `${infer Str extends "aaa"} ${infer Str2}` ? [Str, Str2] : false; // false
//   ^? - type x4 = ["aaa", "aaa bbb"]
type x5 = "aaa aaa bbb" extends `${infer Str} ${infer Str2 extends "bbb"}` ? [Str, Str2] : false; // false
//   ^? - type x5 = false
meager plinth
#

Needs work internally to improve it I bet

idle owl
#

:S

meager plinth
#

TypeScript is a work in progress ๐Ÿ˜„ like all good languages

idle owl
#

๐Ÿ™‚

gritty bone
#

ts inference is lazy in these situations so it only matches as few times as possible

#

if you remove the constraint on the second infer, you can see what Str in x1 grabs

grizzled charmBOT
#
type x1 = "aaa " extends `${infer Str}${infer _}` ? Str : false;
//   ^? - type x1 = "a"
meager plinth
grizzled charmBOT
#
type x1 = "" extends `${infer Str}` ? Str : false;
//   ^? - type x1 = ""
gritty bone
#

no because that would mean there's nothing

meager plinth
#

Huh?

#

I guess regex follows the same rules

#

Right or wrong, if that's the standard, then OK

#

There is definitely a very valid concept of "very-non-greedy" where it would match the empty string if possible.

#

Testing regex, it is really weird

#

^([a-z]*?)([a-z]*?)([a-z]*?)
with aaa The first 2 groups will take nothing and the last group will take a