Skip to content

fix: make the TypeScript component type accept its props - #961

Merged
Naturalclar merged 2 commits into
masterfrom
fix/typescript-component-props
Nov 25, 2026
Merged

Naturalclar merged 2 commits into
masterfrom
fix/typescript-component-props

Conversation

@Naturalclar

@Naturalclar Naturalclar commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Overview

Closes #696, closes #706.

The main bug (#696): with React Native 0.71+ typings (re-checked here with React Native 0.88 and @types/react 19), any JSX use of the component fails to type-check:

error TS2607: JSX element class does not support attributes because it does not have a 'props' property.
error TS2786: 'SegmentedControl' cannot be used as a JSX component.

index.d.ts declared the component as Constructor<NativeMethods> & typeof SegmentedControlComponent. TypeScript takes the instance type from the first construct signature, so the class had no props.

A second, hidden problem: NativeMethods isn't exported by React Native 0.88's types; it was renamed to HostInstance. The repo's skipLibCheck: true also skips our own index.d.ts, so the broken import went unreported, and refs had no measure() etc.

Changes (index.d.ts)

  • Component type: it's now a plain declare class SegmentedControl extends React.Component<SegmentedControlProps>. The ref's native methods (measure, measureInWindow, focus, blur, setNativeProps) are merged in through an interface declared in the file. It doesn't import NativeMethods/HostInstance, because each React Native version exports only one of them. useRef<SegmentedControl>(null) keeps working.
  • fontWeight (Typescript error  #706): numeric values (100–900) are accepted, as React Native text styles accept them.
  • onValueChange (Typescript error  #706): it's declared with method syntax, so callers can annotate the value with a narrower type (e.g. (v: 'S' | 'M' | 'L') => …). Existing code is unaffected.
  • values: image values (require('./icon.png'), {uri}) are accepted, as documented in the README and supported at runtime. The Flow type already allowed them. The type also changed from string[] to ReadonlyArray<…>, so as const arrays work.

Type tests (types-test/typecheck.tsx)

pnpm tsc checks them in CI. They cover JSX usage with typed onChange, a typed ref calling measure(), numeric fontWeight, a narrowed onValueChange, and image values. Two @ts-expect-error cases (an unknown prop and a wrongly typed prop) make sure props really are checked, since an any-typed component would accept them. types-test/ isn't in files, so it isn't published. The file name deliberately doesn't match Jest's *.test.* pattern, so Jest doesn't try to run it.

Test Plan

  • With the old index.d.ts, the type tests fail with exactly TS2607/TS2786, plus Property 'measure' does not exist. With the new one, pnpm tsc passes.
  • With skipLibCheck off, index.d.ts itself now has no errors. Before, it had TS2305: Module "react-native" has no exported member 'NativeMethods'. Turning skipLibCheck off repo-wide isn't possible, because React Native 0.88's own generated types report 3 errors.
  • Jest (20 passed), pnpm lint (0 errors) and flow check pass.
  • npm pack --dry-run still lists the same 17 files.

This is a fix: commit, so merging releases 2.6.1.

🤖 Generated with Claude Code

Naturalclar and others added 2 commits November 25, 2026 13:03
`index.d.ts` declared the component as
`Constructor<NativeMethods> & typeof SegmentedControlComponent`. TypeScript
takes the instance type from the first construct signature, so the class
had no `props`, and with React Native 0.71+ typings every JSX usage failed
with TS2607 / TS2786 (#696). `NativeMethods` is also no longer exported by
React Native 0.88's types; `skipLibCheck` hid the broken import.

- Declare a plain `React.Component<SegmentedControlProps>` class and merge
  the ref's native methods in via an interface declared in this file
- Accept numeric `fontWeight` values (#706)
- Declare `onValueChange` with method syntax so it can be annotated with
  a narrower value type (#706)
- Accept image values (`require(...)`, `{uri}`) in `values`, as documented
- Add type-level tests (types-test/), checked by `pnpm tsc` in CI

Closes #696
Closes #706

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Jest picked up types-test/index.test.tsx through its *.test.* pattern and
failed to run it. It is a type-level test, checked by `pnpm tsc`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Naturalclar
Naturalclar merged commit 9d3ad65 into master Nov 25, 2026
6 checks passed
@Naturalclar
Naturalclar deleted the fix/typescript-component-props branch November 25, 2026 05:04
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.

Typescript error upgrade typings to work with react-native 0.71.0

1 participant