Skip to content

Order of expressions in if block affect type narrowing from unknown #34334

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

Closed
frederickfogerty opened this issue Oct 14, 2019 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@frederickfogerty
Copy link

frederickfogerty commented Oct 14, 2019

TypeScript Version: 3.7-Beta

Search Terms: object null order type narrowing

Code

const maybeObject: unknown = {};

if (maybeObject != null && typeof maybeObject === 'object') {
    Object.keys(maybeObject)
}

if (typeof maybeObject === 'object' && maybeObject != null) {
    Object.keys(maybeObject)
}

Playground link

Expected behavior:

No type errors; maybeObject is correctly narrowed to object in both cases.

Actual behavior:

The first if block throws an error as maybeObject is incorrectly narrowed to object | null, whereas the second maybeObject is incorrectly narrowed.

No overload matches this call.
  Overload 1 of 2, '(o: {}): string[]', gave the following error.
    Argument of type 'object | null' is not assignable to parameter of type '{}'.
      Type 'null' is not assignable to type '{}'.
  Overload 2 of 2, '(o: object): string[]', gave the following error.
    Argument of type 'object | null' is not assignable to parameter of type 'object'.
      Type 'null' is not assignable to type 'object'.(2769)

Playground Link: http://www.typescriptlang.org/play/?ts=3.7-Beta&ssl=1&ssc=1&pln=9&pc=2#code/MYewdgzgLgBAtgQwJ4CMCmB5FArNwoBcMArmANZggDuYMAvDAN4C+A3AFDsCWAZjABSJUmHHlgBCBmGIAbGTABkCmFCQAHNCD5D0WXPnp0GAchCj8xgJRN2MOzD1iAdGTRIIg5LvNRL7Zpy8AqoaWvBeIvqwRiZmUcaKyjqRYjCSMNJy1oy29o74Lm4eyfm+-kA

Related Issues: No

@frederickfogerty frederickfogerty changed the title Order of statements in if statement affect type narrowing from unknown Order of expressions in if statement affect type narrowing from unknown Oct 14, 2019
@frederickfogerty frederickfogerty changed the title Order of expressions in if statement affect type narrowing from unknown Order of expressions in if affect type narrowing from unknown Oct 14, 2019
@frederickfogerty frederickfogerty changed the title Order of expressions in if affect type narrowing from unknown Order of expressions in if block affect type narrowing from unknown Oct 14, 2019
@jack-williams
Copy link
Collaborator

Duplicate of #28131

@jack-williams jack-williams marked this as a duplicate of #28131 Oct 14, 2019
@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label Oct 14, 2019
@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.

LotharKAtt added a commit to ExaForce/TypeScript that referenced this issue Mar 17, 2025
LotharKAtt added a commit to ExaForce/TypeScript that referenced this issue Mar 18, 2025
vaclav-dvorak added a commit to ExaForce/TypeScript that referenced this issue Mar 18, 2025
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