question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

String case change methods should return intrinsic string manipulation types

See original GitHub issue

lib 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:open
  • Created 2 years ago
  • Reactions:30
  • Comments:29 (12 by maintainers)

github_iconTop GitHub Comments

10reactions
ajafffcommented, Oct 15, 2021

Just use a generic this type on the method signatures:

interface String {
    toUpperCase<T extends string>(this: T): Uppercase<T>;
}
6reactions
mycarrysuncommented, Mar 28, 2022

This issue is “awaiting more feedback”. This means we’d like to hear from more people who would be helped by this feature, understand their use cases, and possibly contrast with other proposals that might solve the problem in a simpler way (or solve many other problems at once).

If this feature looks like a good fit for you, please use the 👍 reaction on the original post. Comments outlining different scenarios that would be benefited from the feature are also welcomed.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found