Skip to content

isArray type guard regression #41889

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
mjbvz opened this issue Dec 9, 2020 · 3 comments
Closed

isArray type guard regression #41889

mjbvz opened this issue Dec 9, 2020 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@mjbvz
Copy link
Contributor

mjbvz commented Dec 9, 2020

TypeScript Version: 4.2.0-dev.20201208

Search Terms:

Code

interface IAction {
    x: number;
}

function foo(arg: IAction | ReadonlyArray<IAction>) {
    const actions: ReadonlyArray<IAction> = Array.isArray(arg) ? arg : [arg];
}

Expected behavior:
This compiles fine in TS 4.1.2

Actual behavior:
In the latest nightly, this produces this error:

Type '(IAction & any[]) | (readonly IAction[] & any[]) | (IAction | readonly IAction[])[]' is not assignable to type 'readonly IAction[]'.
  Type '(IAction | readonly IAction[])[]' is not assignable to type 'readonly IAction[]'.
    Type 'IAction | readonly IAction[]' is not assignable to type 'IAction'.
      Property 'x' is missing in type 'readonly IAction[]' but required in type 'IAction'.ts(2322)

Playground Link: https://www.typescriptlang.org/play?ts=4.2.0-dev.20201208#code/JYOwLgpgTgZghgYwgAgJIEEFmAexMgbwChlTkAPALmRAFcBbAI2gG4iBfIomWkLXfDBw4AFHCgBzahn55kAH2QAlCHAAmeADYBPdFChxtAHhnY8APgCUhEmQR4AzmGSIzIB9RXqtu-YZOYbubIALzIegbaAHTADhGGYpLWAPwuksjUANriEgC6bOxAA

Related Issues:

@mjbvz
Copy link
Contributor Author

mjbvz commented Dec 9, 2020

The type of arg after the isArray guard ends up being:

(IAction & any[]) | (readonly IAction[] & any[])

@MartinJohns
Copy link
Contributor

Related: #41808. They mentioned they plan to reverse the previous breaking changes with 4.1.3.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 9, 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.

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

No branches or pull requests

4 participants