From 8a0e29fd541da0bfb16e4d97304906e4eb486b85 Mon Sep 17 00:00:00 2001 From: Jesse Katsumata Date: Wed, 25 Nov 2026 13:03:37 +0900 Subject: [PATCH 1/2] fix: make the TypeScript component type accept its props `index.d.ts` declared the component as `Constructor & 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` 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 --- index.d.ts | 53 +++++++++++++++++++++++++++++------- types-test/index.test.tsx | 56 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 9 deletions(-) create mode 100644 types-test/index.test.tsx diff --git a/index.d.ts b/index.d.ts index 2820e3fe..00dec52f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,12 +5,35 @@ import * as React from 'react'; import { ViewProps, NativeSyntheticEvent, - NativeMethods, TargetedEvent, ViewStyle, + ImageSourcePropType, } from 'react-native'; -type Constructor = new (...args: any[]) => T; +/** + * Native methods available on a ref to the control. + * Declared here rather than imported, because React Native renamed its type + * (`NativeMethods` -> `HostInstance`) and only exports one or the other + * depending on the version. + */ +export interface SegmentedControlNativeMethods { + measure( + callback: ( + x: number, + y: number, + width: number, + height: number, + pageX: number, + pageY: number, + ) => void, + ): void; + measureInWindow( + callback: (x: number, y: number, width: number, height: number) => void, + ): void; + focus(): void; + blur(): void; + setNativeProps(nativeProps: object): void; +} export interface NativeSegmentedControlIOSChangeEvent extends TargetedEvent { value: string; @@ -44,7 +67,16 @@ export type FontStyle = { | '600' | '700' | '800' - | '900'; + | '900' + | 100 + | 200 + | 300 + | 400 + | 500 + | 600 + | 700 + | 800 + | 900; }; export interface SegmentedControlProps extends ViewProps { @@ -76,7 +108,9 @@ export interface SegmentedControlProps extends ViewProps { /** * Callback that is called when the user taps a segment; passes the segment's value as an argument */ - onValueChange?: (value: string) => void; + // Method syntax (rather than a function-typed property) so callers can + // annotate the value with a narrower type, e.g. a union of their labels. + onValueChange?(value: string): void; /** * The index in props.values of the segment to be (pre)selected. @@ -96,8 +130,9 @@ export interface SegmentedControlProps extends ViewProps { /** * The labels for the control's segment buttons, in order. + * Each value is a string, or an image (e.g. `require('./icon.png')`). */ - values?: string[]; + values?: ReadonlyArray; /** * (iOS 13+ only) @@ -145,7 +180,7 @@ export interface SegmentedControlProps extends ViewProps { * ```` */ -declare class SegmentedControlComponent extends React.Component {} -declare const SegmentedControlBase: Constructor & - typeof SegmentedControlComponent; -export default class SegmentedControl extends SegmentedControlBase {} +declare class SegmentedControl extends React.Component {} +// Instances (refs) also expose the native methods (measure, focus, ...). +interface SegmentedControl extends SegmentedControlNativeMethods {} +export default SegmentedControl; diff --git a/types-test/index.test.tsx b/types-test/index.test.tsx new file mode 100644 index 00000000..1a375c86 --- /dev/null +++ b/types-test/index.test.tsx @@ -0,0 +1,56 @@ +// Type-level tests for index.d.ts. Checked by `pnpm tsc` (no runtime code). +import * as React from 'react'; +import SegmentedControl from '../index'; + +// #696: the component accepts its props when used as a JSX element. +export const Basic = () => ( + { + const index: number = event.nativeEvent.selectedSegmentIndex; + const value: string = event.nativeEvent.value; + return [index, value]; + }} + disabledIndices={[1]} + /> +); + +// Props are actually type-checked (an `any`-typed component would accept these). +// @ts-expect-error -- unknown prop +export const UnknownProp = () => ; +// @ts-expect-error -- wrong type for selectedIndex +export const WrongType = () => ; + +// A ref exposes the native methods. +export const WithRef = () => { + const ref = React.useRef(null); + React.useEffect(() => { + ref.current?.measure(() => {}); + }, []); + return ; +}; + +// #706: numeric fontWeight, as accepted by React Native text styles. +export const NumericFontWeight = () => ( + +); + +// #706: onValueChange can be typed with a narrower value type. +type Size = 'S' | 'M' | 'L'; +export const NarrowedValue = () => ( + v} /> +); + +// Image values, as documented in the README. +export const ImageValues = () => ( + +); + +declare function require(path: string): number; From 06287c013cb3bb3693721decc9403021350d87a3 Mon Sep 17 00:00:00 2001 From: Jesse Katsumata Date: Wed, 25 Nov 2026 13:07:17 +0900 Subject: [PATCH 2/2] test: rename the type tests so Jest does not run them 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 --- types-test/{index.test.tsx => typecheck.tsx} | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) rename types-test/{index.test.tsx => typecheck.tsx} (88%) diff --git a/types-test/index.test.tsx b/types-test/typecheck.tsx similarity index 88% rename from types-test/index.test.tsx rename to types-test/typecheck.tsx index 1a375c86..ad55feba 100644 --- a/types-test/index.test.tsx +++ b/types-test/typecheck.tsx @@ -1,4 +1,6 @@ -// Type-level tests for index.d.ts. Checked by `pnpm tsc` (no runtime code). +// Type-level tests for index.d.ts, checked by `pnpm tsc` (not a Jest test; the +// file name deliberately does not match Jest's *.test.* pattern). +/* eslint-disable react-native/no-inline-styles -- fontStyle props, not styles */ import * as React from 'react'; import SegmentedControl from '../index';