-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
--strictNullChecks does not gaurd against optional arguments assignment #9450
Comments
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 |
I believe I follow your explanation, for clarity can you post an example from |
@RyanCavanaugh does your explanation describes also my problem or should i open a new issue?
|
@Raynos a typical example looks like this declare function myForEach(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 @HolgerJeromin see https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-functions-with-fewer-parameters-assignable-to-functions-that-take-more-parameters |
I stumbled across the same issue with my React component callback: <FieldMapper
onFieldSelected={this.handleFieldSelected}
/> Types: FieldMapperProps {
onFieldSelected(discriminator: string, field: Field | undefined): void;
...
}
SomeComponentClass ...
handleFieldSelected(discriminator: string, field: Field): void;
... Here, I am aware of the design decision about parameter arity you made in the past, but I still hope this can be changed for |
It's tempting to make this change, but the gains would be largely illusory due to parameter bivariance (see FAQ). Since |
I forgot about parameter bivariance. Optional parameters should behave identical to |
TypeScript Version: Version 1.9.0-dev.20160610-1.0
Code
Expected behavior:
Expected "Cannot call foo() with undefined"
Actual behavior:
No errors output.
When running
node wat.js
we get an expected uncaught exceptionMy 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.The text was updated successfully, but these errors were encountered: