Skip to content

onChange type breaks after updating to 9.1.0 #6202

@xec

Description

@xec

After updating from 8.10.0 to 9.1.0 typechecking breaks our build, caused by a change to the discriminating union determining the type of onChange.

To Reproduce

  1. Go to Codesandbox: https://codesandbox.io/p/sandbox/datepicker-onchange-type-ddjhws
  2. Look at the type inferred for the onChange function

as long as typescript is in strict mode (or "noImplicitAny" is enabled) the following code will no longer compile:

<DatePicker
  onChange={(x) => {
    // typechecking fails - x is "any" in 9.1.0 :(
    // expected x to be "Date | null"
  }}
/>

Error message:

Parameter 'x' implicitly has an 'any' type. typescript(7006)

Expected behavior

Expected the inferred type of onChange first parameter to be Date | null as it was in 8.10.0 and 9.0.0

If either selectsRange or selectsMultiple is true, the onChange type is inferred correctly as expected.

Screenshots

Image

Additional context

Explicitly specifying the parameter type inline would silence the error for this specific instance:

<DatePicker
  onChange={(x: Date | null) => {
    // ...
  }}
/>

Unfortunately this is not an option for us since it's part of a distributed (re-styled) wrapper and we want to avoid breaking sub-dependant projects :(

Possible fix

Looks like the change was introduced here:
3a82fd1

Adding the ? to selectsMultiple?: true means it accepts undefined as well, not only when selectsMultiple is true, making typescript unable to determine which part of the union should apply since they all match when undefined.

Removing the question marks introduced on lines 260 and 272 in the commit linked above seems to resolve the issue.


Edit: the type appears to work fine in 9.0.0 as well

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions