Repository navigation
fix: type-check color scheme writes on every react-native in the peer range - #586
Merged
Merged
Conversation
… range react-native's ColorSchemeName changes inside the >=0.81 peer range: 0.81 declares 'light' | 'dark' | null | undefined, 0.82-0.86 'light' | 'dark' | 'unspecified', and 0.87+ narrows it to 'light' | 'dark' and adds 'auto' to setColorScheme. Comparing or passing the "unspecified" literal therefore failed tsc on 0.81 (three places) and on 0.87+ (native colorScheme.set). Keep react-native's own type and narrow at runtime instead: a value that is not 'light' or 'dark' means "follow the system". The one place that must hand that to Appearance passes "unspecified", the only reset value every version's native module accepts, asserted to the installed setColorScheme parameter type. Runtime behaviour is unchanged for every value main accepted; "auto" now also clears the override instead of being stored as the scheme. Co-authored-by: Yevhenii <evgeny.kotyrlo@gmail.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
danstepanov
approved these changes
Nov 23, 2026
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.
Supersedes #429. Credit to @YevheniiKotyrlo, who found the problem and mapped the version differences; this takes a smaller approach to the same bug that maintains usage of the React Native types while also covering newer versions of React Native.
Problem
peerDependenciesallowsreact-native >=0.81, but react-native'sColorSchemeNamechanges inside that range, and the package only type-checks against 0.82–0.86:ColorSchemeNameAppearance.setColorSchemeaccepts'light' | 'dark' | null | undefined'light' | 'dark' | 'unspecified''light' | 'dark''auto', deprecates'unspecified''light' | 'dark''light' | 'dark' | 'auto' | 'unspecified'On 0.81,
tscfails insrc/jest/index.ts,src/web/api.tsx(TS2345) andsrc/native/api.tsx(TS2367). On 0.87 and later,src/native/api.tsxstill fails, because comparing against"unspecified"no longer overlaps with the type.Apps type-check against the published
.d.tsfiles, so this breaks building and type-checking this package against those versions, not apps that use it. Runtime behaviour is unaffected.Solution
Keep react-native's own
ColorSchemeNameand narrow at runtime, instead of redeclaring a union that has to track every release:colorScheme.set: anything other than"light"or"dark"clears the override (null), which means "follow the system".colorScheme.setand the jest reset: pass"light"/"dark"through, and otherwise pass"unspecified", asserted to the installedsetColorSchemeparameter type."unspecified"is the only reset value that works at runtime on every version: 0.81 mapsnullto it before calling the native module, 0.82+ pass it straight through, and Android'sAppearanceModuleignores"auto"before 0.88.The assertion needs no
as unknown: TypeScript widens the literal tostringbefore checking it against the parameter type, so it compiles on 0.81, where"unspecified"isn't in the declared union.The public
colorScheme.settype follows the installed react-native:nullmeans "follow the system" on every version, and"unspecified"type-checks only where react-native declares it (0.82–0.86).Behaviour
Unchanged for every value
mainaccepted. The one difference:"auto"(0.87+) now clears the override, wheremainstored it as the scheme andcolorScheme.get()returned"auto".Tests
src/__tests__/native/media-query.test.tsx: aftercolorScheme.set("dark"), each ofnull,undefined,"unspecified"and"auto"returnsprefers-color-schemerules to the system scheme, andcolorScheme.get()reports it. The"auto"case fails onmain.Verification
tsc --noEmitclean against react-native 0.86.3 (installed), and against 0.81.4's and 0.87.0'sAppearance.d.ts.main), which are outside this PR.yarn test: 104 suites, 1923 tests pass.yarn lintclean on the changed files.