Skip to content
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

that !== null && typeof that === "object" still claims that that can be null #40363

Closed
derolf opened this issue Sep 3, 2020 · 3 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@derolf
Copy link

derolf commented Sep 3, 2020

TypeScript Version: 4.0.2

Search Terms:

typeof, object, narrow, if, null

Code

function broken(that: unknown): object {
  if (that !== null && typeof that === "object") {
    return that;  // <--- ts(2322)
  }
  return {};
}

function works(that: unknown): object {
  if (typeof that === "object" && that !== null) {
    return that;
  }
  return {};
}

Expected behavior:

Compile.

Actual behavior:

Fails with error

Type 'object | null' is not assignable to type 'object'.
  Type 'null' is not assignable to type 'object'.ts(2322)

Playground Link:

https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABAIwE5wNYFMwAooAWAhlAFyLgZhwDuYAlOXMgFZbSIDeAUIojMET5iURAEIAvBMRgQAGzmIAZEsRQAngAcscQYRKIp0gETM20Y-S68+iVFighUSfVADcNgL437j5108Pb25QSFgERBo4VAwAZ2ESckpqOkZEM3ZRHj4BIQ1tXTURQylEU1ZM42VVV3FS2QUrbNtfJxcRDz5vPlb-TkDuTyA

@MartinJohns
Copy link
Contributor

Duplicate of #28131. Search terms used: null object

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 9, 2020
@RyanCavanaugh
Copy link
Member

Thanks @MartinJohns!

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants