Skip to content

fix(types): include optional nativeEvent.target on picker events - #1064

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

fix(types): include optional nativeEvent.target on picker events#1064
cpruijsen wants to merge 1 commit into
react-native-datetimepicker:masterfrom
cpruijsen:fix/issue-927

Conversation

@cpruijsen

Copy link
Copy Markdown

Summary

Adds optional target?: number to DateTimePickerEvent and DateTimePickerChangeEvent (TypeScript and Flow) and documents it. Runtime event construction is unchanged: Android still omits the field; iOS dismiss still omits it.

Fixes #927.

iOS onChange / onValueChange events include nativeEvent.target (the React tag React Native injects on native view events); Android dialogs omit it, because the picker is not a view. The shared TypeScript type only listed timestamp and utcOffset, so accessing target was a type error even though iOS returns it. Reporter: compact mode on iOS, Expo SDK 51, intersecting DateTimePickerEvent with { nativeEvent: { target?: number } } to compile.

The types use optional target on the shared event nativeEvent rather than platform-specific event types (IOSDateTimePickerEvent vs AndroidDateTimePickerEvent) or stripping iOS extras so both sides match the old smaller type. Flow already models nativeEvent as an open object; optional target is the smallest TypeScript equivalent, matches the reporter's workaround, and keeps Android object literals assignable. Stripping iOS target would break the reporter. Can switch to platform-specific event types if that is preferred.

Test Plan

What's required for testing (prerequisites)?

A TypeScript consumer of DateTimePickerEvent. No device required for the type change; yarn test test/types.test.js typechecks a fixture under tsc --strict.

What are the steps to reproduce (after prerequisites)?

  1. Assign onChange as (event: DateTimePickerEvent) => { event.nativeEvent.target }.
  2. Before: TS2339 Property 'target' does not exist.
  3. After: target is number | undefined. An Android-shaped event { type: 'set', nativeEvent: { timestamp, utcOffset } } still typechecks.

Optional on a simulator: compact iOS picker, log event.nativeEvent on change: target is present; Android dialog change: target is absent.

Compatibility

OS Implemented
iOS
Android

Checklist

  • I have tested this on a device and a simulator
  • I added the documentation in README.md
  • I updated the typed files (TS and Flow)
  • I added a sample use of the API in the example project (example/App.js)
  • I have added automated tests, either in JS or e2e tests, as applicable

iOS native view events include the React tag RN injects as nativeEvent.target,
but the shared TypeScript type omitted it, so accessing that field was a type
error even though the value is present at runtime.
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.

IOS and ANDROID onChange Event do not share the same value but have the same type

1 participant