-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Allow implicit undefined
returns when the contextual union type contains it
#57912
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
base: main
Are you sure you want to change the base?
Allow implicit undefined
returns when the contextual union type contains it
#57912
Conversation
@typescript-bot test top400 @typescript-bot perf test this faster |
Hey @jakebailey, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
Hey @jakebailey, the results of running the DT tests are ready. There were interesting changes: Branch only errors:Package: nodes7
|
@jakebailey Here are the results of running the user tests comparing Everything looks good! |
@jakebailey Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
I think there's a possible parsing ambiguity in the referenced comment
|
@jakebailey Here are the results of running the top 400 repos comparing Everything looks good! |
The single reported failure here comes from those lines here. We can quickly verify on this TS playground that the contextual type of this function is |
…turn-contextual-union # Conflicts: # tests/baselines/reference/functionsMissingReturnStatementsAndExpressionsStrictNullChecks.types # tests/baselines/reference/inferenceDoesNotAddUndefinedOrNull.types
const f21: () => undefined | number = () => { | ||
~~~ | ||
!!! error TS2322: Type '() => void' is not assignable to type '() => number | undefined'. | ||
!!! error TS2322: Type 'void' is not assignable to type 'number | undefined'. | ||
// Error, regular void function because contextual type for implicit return isn't just undefined | ||
// Ok, contextual type for implicit return contains undefined | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m not the right person to review this, because I personally lean strongly toward the belief that distinctions between return
, return undefined
, and no return belong in a linter and not in TS, which I think is not the consensus view on the team. I can’t tell whether the design meeting notes on this were meant to be descriptive or prescriptive:
But no implicit returns when you have
4 | undefined
If it was intended to be prescriptive, this baseline change violates the intention that was captured in the notes. However, moving in this direction is my preference, so I’m going to approve in hopes that it moves the review along 🤷♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that was descriptive, stating what the current behavior is and that it's inconsistent.
fixes #57840
@RyanCavanaugh triaged this as a bug here. I'm pretty sure that @ahejlsberg didn't want this to work this way though (as per the description of #53607 and the comment in tests that I had to touch here).