String case change methods should return intrinsic string manipulation types
See original GitHub issuelib Update Request
Configuration Check
My compilation target is ES2018
and my lib is ["ES2016", "dom"]
.
Missing / Incorrect Imprecise Definitions
At least toUpperCase
and toLowerCase
methods on String
, maybe/probably also the Locale
versions?
Sample Code
type LittleT = "t" | "tt";
type BigT = Uppercase<LittleT>;
declare const smol: LittleT;
const big: BigT = smol.toUpperCase(); // err, string is not assignable to "T" | "TT
Documentation Link
https://tc39.es/ecma262/#sec-string.prototype.tolowercase
Note
I’m not sure about the Locale
versions of these methods because I don’t know what algorithm the “intrinsic” Uppercase<T>
/ Lowercase<T>
helper types are required to follow. (Do we need separate LocaleUppercase
/ LocaleLowercase
helpers?)
Also worth mentioning: I think what I’m looking for is a return type of e.g. Uppercase<this>
, which shouldn’t have an impact on non-literal string types because Uppercase<string>
is just string
.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:30
- Comments:29 (12 by maintainers)
Top Results From Across the Web
String manipulation types in TypeScript 4.1
A few possible use cases: ${keyof T}-changed to infer "foo-changed"-style events; Enumerating combinations of string literal types as above ...
Read more >Documentation - Template Literal Types - TypeScript
Generating mapping types which change properties via template literal strings.
Read more >Intrinsic String Manipulation Types In TypeScript
Learn about the Intrinsic String Manipulation Types in TypeScript: Uppercase, Lowercase, Capitalize and Uncapitalize with simple code ...
Read more >Intrinsic Types in TypeScript - Level Up Coding - gitconnected
What are intrinsic types (Uppercase, Lowercase, Capitalize, Uncapitalize) needed ... Also, what other string operations could one think of?
Read more >Template Literal Types - 《TypeScript v4.2 Documentation》
String Unions in Types · Inference with Template Literals · Intrinsic String Manipulation Types. Uppercase<StringType>. Example · Lowercase< ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Just use a generic
this
type on the method signatures:Wanted to comment as you mentioned you wanted to hear if others find this useful. I was searching for a workaround for this exact scenario so yes it helps me a lot. We are eslint banning type assertions in our code base so would rather not have to do type assertions and augmenting built in types is not a great solution. Plus this is the expected behavior since this is how the function actually works.