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

Order of type guards in &&-statement changes inferred type #39653

Closed
hhaentze opened this issue Jul 18, 2020 · 2 comments
Closed

Order of type guards in &&-statement changes inferred type #39653

hhaentze opened this issue Jul 18, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@hhaentze
Copy link

TypeScript Version: 3.5.3, 3.9.2, 4.0.0-beta

Search Terms: type guard, incorrect type

Code

function doSomething(value: unknown): object {
  
  if (value !== null && typeof value === 'object'){
    return value //value: object | null
  } 
  if (typeof value === 'object' && value !== null){
    return value //value: object
  } 
  return {}
}

Expected behavior:
The variable "value" is correctly narrowed down to object in both cases

Actual behavior:
The variable is narrowed down to "object | null" in the first case and to only "object" in the second case.

Playground Link: test on playground

@MartinJohns
Copy link
Contributor

Duplicate of #28131. Used search terms: narrow null object

@orta orta added the Duplicate An existing issue was already created label Jul 18, 2020
@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