-
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
Doesn't work type checking for function arguments #11058
Comments
I guess what's happening here: since Indeed it's possible to write: 'foo' as {}; // ok |
As @nippur72 points out, this is a consequence of function argument bivariance, where if a parameter expects a type In this case, the type What you seem to want in this example is parameter invariance, i.e. the exact type with no extra props. TS doesn't support that. |
An interface with all optional arguments is just like one that is empty. an empty interface matches anything, including strings. see https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-all-types-assignable-to-empty-interfaces for details. |
It is not intuitive behavior. Flowjs works as expected https://flowtype.org/try/#0GYVwdgxgLglg9mABMMAKOAHWCDOAuRAb0QEMB+AsEAWwCMBTAJwBpFaK244AbekpAL4BKIgChG9KCEZJM2MDgB0JRAB9ViAIwBuRKIGjRKVAHJgXE0O1A |
@mhegazy you right, however at the same time TS disallows "extra" properties: function fn(options: { a?: number, b?: boolean }) {
return options.a || 1;
}
fn('foo'); // ok
fn({ c: 1; }); // error even though { c: number; } is assignable to { a?: number; b?: boolean; } the reason you do this is to catch common errors, right? so must be done here. |
TypeScript Version: 1.8.0 / nightly (2.0.0-dev.201xxxxx)
Code
Expected behavior:
Must be an error in
Actual behavior:
No errors, TS compiler say "OK".
The text was updated successfully, but these errors were encountered: