ignore infer template literal omit name template literal infer
Can we omit the inferred names in string template literal types if we don't need their values?
Suppose you want to use a template literal type to find strings matching a template, but don't care what's in those inferred strings. You could use a name like ${infer _}, but that's a little extra verbosity.
type OnlyKeysContainingSmoosh<T> = keyof {
[Key in keyof T as Key extends `${infer _}Smoosh${infer _}` ? Key : never]: T[Key];
};
// "Smoosh" | "PreSmoosh" | "SmooshPost"
type FilteredKeys = OnlyKeysContainingSmoosh<{
Smoosh: 1,
PreSmoosh: 2,
SmooshPost: 3,
NoKeyForYou: 4,
}>;
The proposal here would allow omitting the _ and _ in the above example:
type OnlyKeysContainingSmoosh<T> = keyof {
[Key in keyof T as Key extends `${infer}Smoosh${infer}` ? Key : never]: T[Key];
}
_(Separately, I also find it spooky that it's ok to have the same name for the two inferred types, but that might just be a separate issue...)_
My suggestion meets these guidelines:
I think you can now write string in those slots and it will work kind of like an existential type parameter (e.g. ? extends string). Is that what you want?
Oh geez. My TSConf 2020 talk is going to be a little outdated... thanks! 馃槄
Just use a lockfile and you'll be good 馃檪
Most helpful comment
Just use a lockfile and you'll be good 馃檪