Skip to content

Safe guard for elements of fixed array yields error when trying to use whole array #32632

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
G07cha opened this issue Jul 31, 2019 · 1 comment

Comments

@G07cha
Copy link

G07cha commented Jul 31, 2019

TypeScript Version: 3.6.0-dev.20190730

Search Terms:

  • Type guard for items of fixed array
  • Validating items of fixed array

Code

const foo: [Date | undefined, Date | undefined] = [undefined, new Date()]

const strictDatePair = (dates: [Date, Date]) => console.log(dates)

if (foo[0] instanceof Date && foo[1] instanceof Date) {
    strictDatePair([foo[0], foo[1]]) // ok
}

if (foo[0] instanceof Date && foo[1] instanceof Date) {
    strictDatePair(foo) // error TS2345: Argument of type '[Date | undefined, Date | undefined]' is not assignable to parameter of type '[Date, Date]'.
}

Expected behavior:

If item is narrowed to a specific type it should be reflected in array

Actual behavior:

It's only related to individual item

Playground Link: https://www.typescriptlang.org/play/index.html#code/MYewdgzgLgBAZiEAuGBtAIgQygUxgHxgFcwATHOASzB1IBoYtcDiyLraBdGAXjRPJUa9GDQDujbDgAUASk4AoBaEixoAJ0rAoTHAAVMldbxjTSUiCgxSGuzrN4A+GCoggANjgB07kAHMzC1klSjhpBBBUAAZuamhMMGAcEDhJZgAydPhEVABGWNUEpJS0nAcAbwUYapgNLR0pAyNpVAjozgY2-PsFAF8QsLaYmDioIuTU3RhM7Mj8kcLEidKKqpq67V0m9XDEYN6gA

Related Issues: No

@nmain
Copy link

nmain commented Jul 31, 2019

This is a duplicate of #31755, even though the tuple type makes it look a bit different.

@G07cha G07cha closed this as completed Jul 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants