Infer optional types for IIFE parameters with missing arguments#13358
Conversation
|
Great! Just playing around with this in TypeScript, and with Maybe this is "by design", and if you put the annotation you need to mark the param as optional (which also gets rid of the error), but intuitively it feels like this should be valid with the new inference. Thanks! (function(a: string, b: undefined) {
})("test"); |
|
@billti Yes, this is by design. We only contextually type un-annotated parameters. Once a type annotation is present you must also add a |
| links.resolvedSignature = anySignature; | ||
| const type = indexOfParameter < iife.arguments.length ? | ||
| getWidenedLiteralType(checkExpression(iife.arguments[indexOfParameter])) : | ||
| parameter.initializer ? undefined : undefinedWideningType; |
There was a problem hiding this comment.
@sandersn this will need to be covered in your change for initialized parameters.
We currently contextually type un-annotated IIFE parameters by their corresponding arguments. This PR expands upon this concept by inferring un-annotated IIFE parameter with missing arguments as optional. That allows us to handle the following common idiom with no errors:
Without this PR the example is an error because the
undefinedisn't marked optional.Note that the PR also simplifies some overly complicated logic for computing the
minArgCountproperty in signatures.