-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Using startsWith & endsWith to narrow type #46958
Comments
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 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. |
Fair enough, thanks for taking the time to explain |
Here is a declaration that does it: interface String {
startsWith<P extends string>(searchString: P): this is `${P}${string}`;
startsWith<P extends string>(searchString: P, position: 0): this is `${P}${string}`;
} I created an MR to add this to the ts-reset package: mattpocock/ts-reset#161 |
lib Update Request
Configuration Check
My compilation target is
ES2020
and my lib isES2020
.Missing / Incorrect Definition
startsWith
&endsWith
doesn't narrow the the type, it just returns a boolean.Sample Code
Documentation Link
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
Pull Request
#46959 🚀
The text was updated successfully, but these errors were encountered: