Skip to content

Narrow String#toLowerCase/toUpperCase return types via Lowercase/Uppercase#3711

Open
ljharb wants to merge 1 commit intomicrosoft:mainfrom
ljharb:lib-string-tocase-literal-types
Open

Narrow String#toLowerCase/toUpperCase return types via Lowercase/Uppercase#3711
ljharb wants to merge 1 commit intomicrosoft:mainfrom
ljharb:lib-string-tocase-literal-types

Conversation

@ljharb
Copy link
Copy Markdown

@ljharb ljharb commented May 5, 2026

Make String.prototype.toLowerCase and toUpperCase preserve string-literal types in their return type, by typing them as <T extends string>(this: T): Lowercase<T> and Uppercase<T> respectively.

For non-literal string receivers, Lowercase<string> resolves to string, preserving existing behavior. For literal receivers (e.g. "FOO".toLowerCase()), the result narrows to the corresponding literal ("foo").

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

…rcase

Make String.prototype.toLowerCase and toUpperCase preserve string-literal types in their return type,
by typing them as `<T extends string>(this: T): Lowercase<T>` and `Uppercase<T>` respectively.

For non-literal `string` receivers, `Lowercase<string>` resolves to `string`, preserving existing behavior.
For literal receivers (e.g. `"FOO".toLowerCase()`), the result narrows to the corresponding literal (`"foo"`).
@ljharb ljharb force-pushed the lib-string-tocase-literal-types branch from 8cb1144 to 02d97fe Compare May 5, 2026 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants