-
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
Infer extra parameter types from default values #17559
Comments
Thanks for linking to that @olegdunkan! It's indeed the specified behavior, though it's certainly surprising and it looks like |
This spec seems to say about default behaviors that has no default value. @ahejlsberg Does this spec mean that type inference has to ignore a type of default value too and still should be so even today? |
Seems like a spec bug: |
@falsandtru how did you even end up in this state? |
To define and initialize local variables. For example, const count = ((a = 0) => () => ++a)(); can be used as shorthand for const count = (() => {
let a = 0;
return () => ++a;
})(); |
Sorry, my previous example is wrong. Here is a correct one. f((a = 0) => ++a);
function f(cb: () => void) {
} |
Example in OP now works as hoped |
Aww, I was hoping that that would mean this works: function getOr<T, K extends keyof T, D>(
obj: T,
key: K,
// Type 'undefined' is not assignable to type 'D'.
// 'D' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.(2322)
defaultValue: D = undefined,
): T[K] | D {
if (key in obj) {
return obj[key];
} else {
return defaultValue;
}
}
// $ExpectType string | undefined
getOr(process.env, "SOME_VAR");
// $ExpectType string | null
getOr(process.env, "SOME_VAR", null);
// $ExpectType string
getOr(process.env, "SOME_VAR", "DEFAULT"); |
TypeScript Version: nightly (2.5.0-dev.20170801)
Code
Expected behavior:
a
isnumber
type.Actual behavior:
a
isany
type.The text was updated successfully, but these errors were encountered: