You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My compilation target is ES2020 and my lib is ES2020.
Missing / Incorrect Definition
startsWith & endsWith doesn't narrow the the type, it just returns a boolean.
Sample Code
interfaceBuiltinPaymentMethod{id: `builtin-${string}`
// ...}// ...declareconstid: stringif(id.startsWith('builtin-')){// Here I want `id` to be typed as `builtin-${string}`}
I don't think this is a good fit for the general library. Two main concerns: Changing a function from not-overloaded to overloaded has fairly pervasive effects in areas like contextual typing, generic inference, and how exactly it can be invoked when the method operand is a union. The other is that template string literals are inherently combinatorially explosive, so a call like startsWith(union of 24) && endsWith(union of 24) produces a union of 576 elements, which is bad from a performance and comprehensibility perspective.
Thankfully since this is an additional overload rather than a replacement, you can add it to your own project through declaration merging if those trade-offs are a good fit for your use cases.
interfaceString{startsWith<Pextendsstring>(searchString: P): this is `${P}${string}`;startsWith<Pextendsstring>(searchString: P,position: 0): this is `${P}${string}`;}
Activity
RyanCavanaugh commentedon Nov 30, 2021
I don't think this is a good fit for the general library. Two main concerns: Changing a function from not-overloaded to overloaded has fairly pervasive effects in areas like contextual typing, generic inference, and how exactly it can be invoked when the method operand is a union. The other is that template string literals are inherently combinatorially explosive, so a call like
startsWith(union of 24) && endsWith(union of 24)produces a union of 576 elements, which is bad from a performance and comprehensibility perspective.Thankfully since this is an additional overload rather than a replacement, you can add it to your own project through declaration merging if those trade-offs are a good fit for your use cases.
LinusU commentedon Dec 1, 2021
Fair enough, thanks for taking the time to explain
alexgleason commentedon Aug 10, 2023
Here is a declaration that does it:
I created an MR to add this to the ts-reset package: mattpocock/ts-reset#161
PatrykKuniczak commentedon Mar 18, 2025
There still no place to implement it inside TS?