GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Treat intersection types as potential `never` by lowr · Pull Request #47751 · microsoft/TypeScript

Conversation

@lowr

@lowr LowR (lowr) commented Feb 6, 2022

Copy link
Copy Markdown
Contributor

Fixes #46032

This PR fixes crashes and improves error messages when a function is declared to return a type alias that names an intersection type of object-like types that reduces to never.

Besides #46032, there have been certain circumstances under which using such type aliases as functions' return type emits error messages different from using never directly (playground). This PR improves error messages so that they emit the same errors (aside from elaboration of never intersections). This is actually not really related to #46032, so let me know if I should open a separate issue/PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@lowr
fix(46032): fix false positive in isSimpleTypeRelatedTo()
efe5e3d
When the following conditions are satisfied,
`isSimpleTypeRelatedTo(source, target)` returns true, which is false
positive.

 - `strictNullChecks` is disabled

 - `source` is `undefined`

 - `target` is an empty intersection type which reduces to `never` by
   discriminants

In this case, `isSimpleTypeRelatedTo()` should actually return `false`,
which does *not* mean `source` doesn't relate to `target`, but rather
tells the checker to further investigate their relation.
@typescript-bot TypeScript Bot (typescript-bot) added the For Backlog Bug PRs that fix a backlog bug label Feb 6, 2022
LowR (lowr) added 2 commits February 6, 2022 19:10
@lowr
Reduce types before checking if they are never
0ffde2d
When types are checked if they are `never` by their `TypeFlags`, they
must first be reduced, because intersection types of object-like types
which reduce to `never` don't have `TypeFlags.Never`. See microsoft#36696 for
details.

When a function is declared to return such type and it doesn't contain
any return statement or it only contains return statements without
expression, the error messages are incorrect as the checker sees its
return type as non-`never`.

This commit takes into account that intersection types potentially
reduces to `never` and improves error messages.
@lowr
Add test
fa4c478
@lowr
LowR (lowr) force-pushed the fix/46032 branch from 9cf958b to fa4c478 Compare February 6, 2022 10:11
@RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh) requested a review from Anders Hejlsberg (ahejlsberg) February 8, 2022 16:43
ahejlsberg
Anders Hejlsberg (ahejlsberg) requested changes Feb 9, 2022
View reviewed changes
Comment thread src/compiler/checker.ts
if (t & TypeFlags.AnyOrUnknown || s & TypeFlags.Never || source === wildcardType) return true;
if (t & TypeFlags.Never) return false;
// Intersection types that would be reduced to never may be passed as `target`, thus returning early.
if (t & TypeFlags.Intersection) return false;

@ahejlsberg Anders Hejlsberg (ahejlsberg) Feb 9, 2022

Copy link
Copy Markdown
Member

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.

This isn't quite the right fix, I will put something up in a PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@lowr LowR (lowr) Feb 10, 2022

Copy link
Copy Markdown
Contributor Author

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.

Thanks for the review. Besides my fix not allowing empty array literals assigned to never[], is it not "quite the right fix" because it fails to catch cases where target is a union of empty intersections that also reduce to never? A bit of explanation or even pointers would be appreciated if you don't mind!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ahejlsberg Anders Hejlsberg (ahejlsberg) Feb 13, 2022

Copy link
Copy Markdown
Member

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.

Yes, one issue is that both unions and intersections can potentially reduce to never, so both need to be excluded when --strictNullChecks is disabled. But another issue is that any should be assignable to anything, which is handled by a test later in the body of isSimpleTypeRelatedTo. So the code can't just unconditionally return false here.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@lowr LowR (lowr) Feb 15, 2022

Copy link
Copy Markdown
Contributor Author

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.

Thanks! I'll think about those logic more carefully next time.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/compiler/checker.ts
const functionFlags = getFunctionFlags(func);
const type = returnType && unwrapReturnType(returnType, functionFlags);
const unwrappedType = returnType && unwrapReturnType(returnType, functionFlags);
const type = unwrappedType && getReducedType(unwrappedType);

@ahejlsberg Anders Hejlsberg (ahejlsberg) Feb 9, 2022

Copy link
Copy Markdown
Member

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 think this is best covered in a separate PR since it is really an unrelated fix.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

lowr reacted with eyes emoji
All reactions
  • 👀 1 reaction
@ahejlsberg

Anders Hejlsberg (ahejlsberg) commented Feb 9, 2022

Copy link
Copy Markdown
Member

LowR (@lowr) Thanks for your work on this issue. My suggested fix is in #47816. Feel free to copy from that into this PR, or we can just merge mine.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lowr LowR (lowr) mentioned this pull request Feb 10, 2022
Empty intersections as function return types are not treated as never #47833
Open
@lowr LowR (lowr) closed this Feb 15, 2022
@typescript-bot TypeScript Bot (typescript-bot) assigned Anders Hejlsberg (ahejlsberg) Feb 15, 2022
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@ahejlsberg Anders Hejlsberg (ahejlsberg) ahejlsberg requested changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@ahejlsberg Anders Hejlsberg (ahejlsberg)

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Crash trying to assing undefined[] to never[]

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@lowr @ahejlsberg @typescript-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.