Repository navigation
fix: make the TypeScript component type accept its props - #961
Merged
Merged
Conversation
`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>
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.
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/react19), any JSX use of the component fails to type-check:index.d.tsdeclared the component asConstructor<NativeMethods> & typeof SegmentedControlComponent. TypeScript takes the instance type from the first construct signature, so the class had noprops.A second, hidden problem:
NativeMethodsisn't exported by React Native 0.88's types; it was renamed toHostInstance. The repo'sskipLibCheck: truealso skips our ownindex.d.ts, so the broken import went unreported, and refs had nomeasure()etc.Changes (
index.d.ts)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 importNativeMethods/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 fromstring[]toReadonlyArray<…>, soas constarrays work.Type tests (
types-test/typecheck.tsx)pnpm tscchecks them in CI. They cover JSX usage with typedonChange, a typed ref callingmeasure(), numericfontWeight, a narrowedonValueChange, and image values. Two@ts-expect-errorcases (an unknown prop and a wrongly typed prop) make sure props really are checked, since anany-typed component would accept them.types-test/isn't infiles, 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
index.d.ts, the type tests fail with exactly TS2607/TS2786, plusProperty 'measure' does not exist. With the new one,pnpm tscpasses.skipLibCheckoff,index.d.tsitself now has no errors. Before, it hadTS2305: Module "react-native" has no exported member 'NativeMethods'. TurningskipLibCheckoff repo-wide isn't possible, because React Native 0.88's own generated types report 3 errors.pnpm lint(0 errors) andflow checkpass.npm pack --dry-runstill lists the same 17 files.This is a
fix:commit, so merging releases 2.6.1.🤖 Generated with Claude Code