sindresorhus / ts-extras

Essential utilities for TypeScript projects
MIT License
587 stars 15 forks source link

Proposal: add `toLowerCase()` and `toUpperCase()` #34

Open jonahsnider opened 2 years ago

jonahsnider commented 2 years ago

String.prototype.toLowerCase() and String.prototype.toUpperCase() are typed as returning string. This doesn't make use of the relatively recently added Uppercase<T> and Lowercase<T> types in TypeScript.

I propose adding two functions to this library:

function toLowerCase<T extends string>(string: T): Lowercase<T>;
function toUpperCase<T extends string>(string: T): Uppercase<T>;
tychenjiajun commented 2 years ago

If both methods should be added, I propose adding endsWith, startsWtih, padEnd, padStart, repeat, trimEnd, trimStart, trim and so on. https://github.com/microsoft/TypeScript/blob/main/tests/cases/conformance/types/literal/templateLiteralTypes1.ts

sindresorhus commented 2 years ago

How would using this method over String.prototype.toLowerCase() improve your code? Strict types are great, but they got to have a purpose.

jonahsnider commented 2 years ago

Personally I don't have any use case for either of the proposed case functions. However there are surely developers who could benefit from using one of them instead of just asserting the type after using the built-in method.

sindresorhus commented 2 years ago

Let's keep this open for more feedback. I would like to see more realistic use-cases.