-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Contravariance with optional arguments causes runtime errors #59096
Comments
It's not exactly about variance, but yes, optionality and aliasing mix pretty poorly in some contexts, and it comes up with objects too. There isn't much we're planning to do here, it is an intentional trade-off in usability and soundness. The closest thing I can find in the FAQ is https://github.com/microsoft/TypeScript/wiki/FAQ#why-are-functions-with-fewer-parameters-assignable-to-functions-that-take-more-parameters |
The same tradeoff applies to objects, fwiw - itβs not specific to functions.
|
I see, thank you both. I see the support is intentional, but this could cause a variable with an unexpected type gets passed around the code base, which lowers confidence in the advertised type of any variable. Is it possible to at least disallow this kind of behavior in strict mode, or perhaps a lint rule so users can optionally turn it on? |
I think the closest thing would be exact types #12936 because it could(?) disallow the case of But that doesn't cover the original issue around function parameter lists. |
This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
contravariance, function types, crash, optional arguments
π Version & Regression Information
Not sure, but it may cause runtime crash
β― Playground Link
No response
π» Code
π Actual behavior
A function with optional parameters can get assigned to a function variable with less parameters. At first glance this is ok because you'd think
x
can be called withoutsecond
, but it will cause issues when that variable gets assigned to a function variable with more parameters.π Expected behavior
x
should not be assigned toy
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: