Skip to content

fix: stop static Picker.Item from referencing class type parameters - #677

Open
cpruijsen wants to merge 1 commit into
react-native-picker:masterfrom
cpruijsen:fix/issue-307
Open

cpruijsen wants to merge 1 commit into
react-native-picker:masterfrom
cpruijsen:fix/issue-307

Conversation

@cpruijsen

Copy link
Copy Markdown

Type Item as typeof PickerItem with a generic PickerItem class, matching PickerIOS and static Item: typeof PickerItem = PickerItem in js/Picker.js. PickerProps<T> / onValueChange are unchanged.

Fixes #307
Also covers #339

Summary

static Item: React.ComponentType<PickerItemProps<T>> is invalid TypeScript (TS2302: static members cannot reference class type parameters). TypeScript documents this at https://www.typescriptlang.org/docs/handbook/2/classes.html#type-parameters-in-static-members

Same error as #216. #222 fixed it by using ItemValue; #302 (v1.16.5) put T back on Item and the error returned. Still present on current main. Consumers that leave skipLibCheck at the default (false) fail; this repo's tsc job did not catch it because tsconfig.json sets "skipLibCheck": true.

validate:typescript now also runs a check that typechecks typings/ with skipLibCheck: false and only asserts diagnostics in that folder, so this error cannot silently return.

The class is used instead of static Item: React.ComponentType<PickerItemProps<ItemValue>> (the #222 one-liner). The one-liner is enough to clear TS2302 and is what #216 confirmed. The class is the same Item-class pattern already in this repo, and Picker.Item stays generic (the intent of #302) without naming Picker's T on a static. Can switch to the one-liner if that is preferred.

Test plan

  • yarn validate:typescript passes
  • In an app without skipLibCheck: true, tsc no longer reports TS2302 from node_modules/@react-native-picker/picker/typings/Picker.d.ts
  • <Picker.Item label="Java" value="java" /> still typechecks
  • Picker selectedValue / onValueChange generics still infer

TypeScript forbids static members from using the class type parameter,
which broke consumer tsc since 1.16.5.
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.

Static members cannot reference class type parameters.

1 participant