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
Infer extra parameter types from default values #17559
Thanks for linking to that olegdunkan! It's indeed the specified behavior, though it's certainly surprising and it looks like noImplicitAny doesn't catch it.
This spec seems to say about default behaviors that has no default value. Anders Hejlsberg (@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: considered to have type Any should probably be whatever spec language would be for considered to have the type that would be inferred without a contextual signature.
Aww, I was hoping that that would mean this works:
functiongetOr<T,KextendskeyofT,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(keyinobj){returnobj[key];}else{returndefaultValue;}}// $ExpectType string | undefinedgetOr(process.env,"SOME_VAR");// $ExpectType string | nullgetOr(process.env,"SOME_VAR",null);// $ExpectType stringgetOr(process.env,"SOME_VAR","DEFAULT");
Activity
olegdunkan commentedon Aug 2, 2017
When a function expression with no type parameters and no parameter type annotations is contextually typed (section 4.23) by a type T and a contextual signature S can be extracted from T, the function expression is processed as if it had explicitly specified parameter type annotations as they exist in S. Parameters are matched by position and need not have matching names. If the function expression has fewer parameters than S, the additional parameters in S are ignored. If the function expression has more parameters than S, the additional parameters are all considered to have type Any.
Isn't that case?
DanielRosenwasser commentedon Aug 2, 2017
Thanks for linking to that olegdunkan! It's indeed the specified behavior, though it's certainly surprising and it looks like
noImplicitAnydoesn't catch it.falsandtru commentedon Aug 2, 2017
This spec seems to say about default behaviors that has no default value. Anders Hejlsberg (@ahejlsberg) Does this spec mean that type inference has to ignore a type of default value too and still should be so even today?
simonbuchan commentedon Oct 8, 2017
Seems like a spec bug:
considered to have type Anyshould probably be whatever spec language would be forconsidered to have the type that would be inferred without a contextual signature.RyanCavanaugh commentedon Oct 10, 2017
falsandtru (@falsandtru) how did you even end up in this state?
falsandtru commentedon Oct 10, 2017
To define and initialize local variables. For example,
can be used as shorthand for
falsandtru commentedon Oct 31, 2017
Sorry, my previous example is wrong. Here is a correct one.
RyanCavanaugh commentedon Jun 23, 2021
Example in OP now works as hoped
simonbuchan commentedon Jun 24, 2021
Aww, I was hoping that that would mean this works: