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

Incorrect tuple union type deduction #29998

Closed
pavel opened this issue Feb 20, 2019 · 2 comments
Closed

Incorrect tuple union type deduction #29998

pavel opened this issue Feb 20, 2019 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@pavel
Copy link

pavel commented Feb 20, 2019

TypeScript Version: 3.3.3 and 3.4.0-dev.20190220 with --strictNullChecks

Search Terms:
tuple, union, type deduction, destructuring

Code

function fn(): [null, number] | [string, null] {
  if (Math.random() > 0.5) {
    return [null, 2]
  }
  return ["Str", null]
}

// l: string | null (correct)
// r: number | null (correct)
const [l, r] = fn()
if (l === null) {
  // r: number | null (incorrect)
  console.log(r + 2)
} else {
  // l: string (correct)
  // r: number | null (incorrect)
  console.log(l.toLowerCase(), r === null)
}

Expected behavior:
No compilation errors. Correct type hints.

Actual behavior:
Compilation error:

TS2531: Object is possibly 'null'.

at console.log(r + 2). Incorrect type hints.

Playground Link:
Playground link. Make sure to enable "strictNullChecks".

Related Issues:
Did not find anything exactly like this issue.

@jack-williams
Copy link
Collaborator

This is tracked by this #12184. If you go to the linked issue #28981 the example is more-or-less the same as the one you provide.

@pavel
Copy link
Author

pavel commented Feb 20, 2019

Thanks.

@pavel pavel closed this as completed Feb 20, 2019
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 20, 2019
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

3 participants