Skip to content

fix: type-check color scheme writes on every react-native in the peer range - #586

Merged
danstepanov merged 1 commit into
nativewind:mainfrom
sammoore:fix/color-scheme-types
Nov 23, 2026
Merged

danstepanov merged 1 commit into
nativewind:mainfrom
sammoore:fix/color-scheme-types

Conversation

@sammoore

Copy link
Copy Markdown
Contributor

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

peerDependencies allows react-native >=0.81, but react-native's ColorSchemeName changes inside that range, and the package only type-checks against 0.82–0.86:

react-native ColorSchemeName Appearance.setColorScheme accepts
0.81 'light' | 'dark' | null | undefined same
0.82–0.86 'light' | 'dark' | 'unspecified' same
0.87 'light' | 'dark' adds 'auto', deprecates 'unspecified'
0.88+ 'light' | 'dark' 'light' | 'dark' | 'auto' | 'unspecified'

On 0.81, tsc fails in src/jest/index.ts, src/web/api.tsx (TS2345) and src/native/api.tsx (TS2367). On 0.87 and later, src/native/api.tsx still fails, because comparing against "unspecified" no longer overlaps with the type.

Apps type-check against the published .d.ts files, 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 ColorSchemeName and narrow at runtime, instead of redeclaring a union that has to track every release:

  • Native colorScheme.set: anything other than "light" or "dark" clears the override (null), which means "follow the system".
  • Web colorScheme.set and the jest reset: pass "light"/"dark" through, and otherwise pass "unspecified", asserted to the installed setColorScheme parameter type.

"unspecified" is the only reset value that works at runtime on every version: 0.81 maps null to it before calling the native module, 0.82+ pass it straight through, and Android's AppearanceModule ignores "auto" before 0.88.

The assertion needs no as unknown: TypeScript widens the literal to string before checking it against the parameter type, so it compiles on 0.81, where "unspecified" isn't in the declared union.

The public colorScheme.set type follows the installed react-native: null means "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 main accepted. The one difference: "auto" (0.87+) now clears the override, where main stored it as the scheme and colorScheme.get() returned "auto".

Tests

  • src/__tests__/native/media-query.test.tsx: after colorScheme.set("dark"), each of null, undefined, "unspecified" and "auto" returns prefers-color-scheme rules to the system scheme, and colorScheme.get() reports it. The "auto" case fails on main.

Verification

  • tsc --noEmit clean against react-native 0.86.3 (installed), and against 0.81.4's and 0.87.0's Appearance.d.ts.
  • Against 0.88.1's generated types, no color-scheme errors remain. There are 18 unrelated errors from 0.88's generated types (also on main), which are outside this PR.
  • yarn test: 104 suites, 1923 tests pass. yarn lint clean on the changed files.

… 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
danstepanov merged commit 4562bdf into nativewind:main Nov 23, 2026
6 checks passed
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