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
- Go to Codesandbox: https://codesandbox.io/p/sandbox/datepicker-onchange-type-ddjhws
- 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
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
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
onChangefunctionas long as typescript is in strict mode (or "noImplicitAny" is enabled) the following code will no longer compile:
Error message:
Expected behavior
Expected the inferred type of
onChangefirst parameter to beDate | nullas it was in 8.10.0 and 9.0.0If either
selectsRangeorselectsMultipleistrue, theonChangetype is inferred correctly as expected.Screenshots
Additional context
Explicitly specifying the parameter type inline would silence the error for this specific instance:
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
?toselectsMultiple?: truemeans it acceptsundefinedas 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