Repository navigation
fix(String): type forwards as the native string methods - #10229
Closed
serhii-indyrct wants to merge 1 commit into
Closed
serhii-indyrct wants to merge 1 commit into
serhii-indyrct wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 879a41e The changes in this PR will be included in the next version bump. This PR includes changesets to release 33 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
Bundle Size AnalysisGenerated from PR build output; treat the content below as untrusted.
|
Contributor
|
Thanks for the careful work here, @serhii-indyrct. We're closing this without merging because we only accept backward-compatible changes in minor releases, and this one can break existing code at the type level:
We also looked at keeping the original signature as a trailing overload. That still breaks the assignability case, so no subset of the change is fully non-breaking. We may revisit this in a future major release. |
This branch is waiting to be deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Several
Stringfunctions forward to a native string method but type their arguments or result narrower than the method does:replaceandreplaceAlltake only a replacement string, while the native methods also take a replacer function.matchAlltypes each match asRegExpMatchArray, whereindexandinputare optional, whileString.prototype.matchAllyieldsRegExpExecArray, where both are always set. ThematchAllJSDoc example readsmatch.indexand getsnumber | undefined.localeComparetakeslocalesonly as a mutableArray<string>, so a single locale like"sv"doesn't compile.toLocaleLowerCaseandtoLocaleUpperCaserefuse a readonly list and anIntl.Locale.Fix
Each function now takes and returns what the native method does: a replacement string or a replacer function,
RegExpStringIterator<RegExpExecArray>frommatchAll, andIntl.LocalesArgumentfor locales, the typeDateTimealready uses. Nothing changes at runtime, and every new signature is assignable to the old one, so existing code keeps compiling.replaceandreplaceAllbranch on the replacement's type only so each call resolves one of the native overloads; a single call with the union doesn't compile.Tests
Runtime tests in
String.test.tsfor replacer functions, theindexandinputof each match, and the locale arguments. Type tests inString.tst.ts, six of which fail onmain.pnpm lint,pnpm check,pnpm test-types --target '>=5.9',pnpm jsdocs --check,pnpm doctest,pnpm buildwithpnpm check-dist-types, anddeno check .pass.String.test.tspasses on Node, Deno and Bun.