You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
--strictNullChecks does not gaurd against optional arguments assignment #9450
My actual production use case is more complicated and involves assigning interfaces into a generic container class where the generic container class has an interface with optional arguments and the instance of the class that assign into the container has methods with non-optional arguments.
I expected that the structural equality checker would fail for optional arguments vs non-optional arguments when --strictNullChecks is enabled.
We have an unfortunate rule about parameter arity that says optional parameters can be treated as mandatory during assignability. This is because people wrote bad callback types which specified parameters as optional intending to mean "ignorable" (but in reality all parameters are safely ignorable by the callee) when the correct interpretation is "might not be provided".
We can think about turning this rule off under strictNullChecks but it would be a fairly large breaking change; doing a scrub of DefinitelyTyped to remove these bad optionality annotations would probably be required.
declarefunctionmyForEach(arr: Foo[],cb: (elem: Foo,index?: number)=>void): void;// Intended meaning: You "must" accept elem, but "may" ignore index// Actual meaning: I will always provide elem, but might not provide index
Here, onFieldSelected callback can be called with undefined field (clearing field selection), and it is wrong to assign a function that expects a non-nullable field, but TypeScript does not complain. This means I cannot rely on strictNullChecks in any callback, and in React they are used so much.
I am aware of the design decision about parameter arity you made in the past, but I still hope this can be changed for --strictNullChecks to make it usable even with callbacks, as it is really a game changer!
It's tempting to make this change, but the gains would be largely illusory due to parameter bivariance (see FAQ). Since (x: T) => void is already assignable in both directions with (x: T | undefined) => void, the only thing we'd gain is more inconsistency between optionality and | undefined, which should in practice behave as similar as possible.
Activity
RyanCavanaugh commentedon Sep 28, 2016
We have an unfortunate rule about parameter arity that says optional parameters can be treated as mandatory during assignability. This is because people wrote bad callback types which specified parameters as optional intending to mean "ignorable" (but in reality all parameters are safely ignorable by the callee) when the correct interpretation is "might not be provided".
We can think about turning this rule off under
strictNullChecksbut it would be a fairly large breaking change; doing a scrub of DefinitelyTyped to remove these bad optionality annotations would probably be required.Raynos commentedon Sep 29, 2016
Ryan Cavanaugh (@RyanCavanaugh)
I believe I follow your explanation, for clarity can you post an example from
DefinitelyTypedabout one of those "bad callback types".HolgerJeromin commentedon Oct 19, 2016
Ryan Cavanaugh (@RyanCavanaugh) does your explanation describes also my problem or should i open a new issue?
RyanCavanaugh commentedon Oct 19, 2016
Jake Verbaten (@Raynos) a typical example looks like this
Holger Jeromin (@HolgerJeromin) see https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-functions-with-fewer-parameters-assignable-to-functions-that-take-more-parameters
vknez commentedon Oct 21, 2016
I stumbled across the same issue with my React component callback:
Types:
Here,
onFieldSelectedcallback can be called withundefinedfield (clearing field selection), and it is wrong to assign a function that expects a non-nullable field, but TypeScript does not complain. This means I cannot rely onstrictNullChecksin any callback, and in React they are used so much.I am aware of the design decision about parameter arity you made in the past, but I still hope this can be changed for
--strictNullChecksto make it usable even with callbacks, as it is really a game changer!RyanCavanaugh commentedon Oct 31, 2016
It's tempting to make this change, but the gains would be largely illusory due to parameter bivariance (see FAQ). Since
(x: T) => voidis already assignable in both directions with(x: T | undefined) => void, the only thing we'd gain is more inconsistency between optionality and| undefined, which should in practice behave as similar as possible.Raynos commentedon Oct 31, 2016
I forgot about parameter bivariance. Optional parameters should behave identical to
T | undefined. Good call.Igorbek commentedon Nov 4, 2016
Just for tracking, it is another issue that can be solved by variance #10717 #1394