-
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
Add trim
as new intrinsic for use with template literal types
#41283
Comments
Related: #41210 |
This may or may or may not be my initial motivation, but I even if this specific use case is not a good reason, I think the underlying feature request still makes sense. |
Use cases more in line with the intended uses of template string literals would be nice |
@RyanCavanaugh not sure if this is a better use case, but another thing I was playing around with in one of my projects that uses Trim. This is a bit simplified, and not very polished but: I can definitely see the argument that there might not be a good "intended" use case for Trim, while it is a very common operation on strings, the intent of these types was probably more along the lines of property manipulation rather than string manipulation which means there are far fewer places where you might run into white space. The |
How would strings like that make it into the type system in the first place, though? |
As I said above, that example is a bit contrived. function printHelpText() {
const helpText = dedent(`
Some command:
details on how to use id
`)
process.stdout.wirte(helpText)
} which would print something like:
Since I could imagine a use case for having additional formatting functions, that may expect a multi-line string or string with other constraints that could be combined with this to produce a combination of constraints that do actually limit the valid strings you can use, but again I'm not sure how valuable that actually is as a real world use case. I think its fair to table this, or close it until there are use cases with more real world value. |
I ran into a use case that I think would benefit from this today I have arrays of string values that come from an external source, and then they get imported as constants like so: const propertyTypes = [
'Single-Family Home',
'Multifamily Home',
'Apartment',
'Condo',
'Townhome',
] as const;
type PropertyType = typeof propertyTypes[number]; I want to define a type for something that maps these values to an object, with each entry having a record of const propertyTypeAttributes = {
singleFamilyHome: {requiresAuthorization: true, escrowDays: 30}
multifamilyHome: {...}
apartment: {...}
condo: {...}
townhome: {...}
} I can almost define this type as As it is, I have two options:
|
We are adding additional type safety to i18next's The following syntax are all valid:
As you can see, having a built-in way (without having to worry about limitations described in this issue's description) to trim whitespace would greatly simplify handling all the cases. |
I am attempting to parse sanity's groq strings in |
Search Terms
intrinsic
trim
whitespace
possible related: #41210
Suggestion
Add
trim
as a new intrinsic alongside the existing intrinsic types:Uppercase
Lowercase
Capitalize
Uncapitalize
Use Cases
Trim can be implemented with the current types pretty easily by doing something like:
But this can quickly run into depth limits if there is a lot of repeated whitespace.
This can be improved by adding strings of repeated whitespace to the Whitespace type, but will still run into limitations:
This seems like a good case for an intrinsic implementation since it is a fairly simple and common operation on strings.
One use case would be to correctly type template literal helpers that trim leading indent whitespace, but there are probably many cases where trimming whitespace from a string would be useful.
Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: