facebook / facebook/flow

Interface extending Array loses all type safety

Open
#5,772 3 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

```
interface NumberArray extends Array { }

function doSomething(x: NumberArray | null) {
if (Array.isArray(x)) {
printString(x); // Why does this work?
printNumber(x); // Why does this work?
printBoolean(x); // Why does this work?
printArray(x);
}
}

function printString(x: string) {
}

function printNumber(x: number) {
}

function printBoolean(x: boolean) {
}

function printArray(x: Array) {
}
```

If I use `type NumberArray = Array;` then everything works as expected.

I tried this in the TypeScript playground and the errors show up as expected. I tried reading Flow's documentation on interfaces but I couldn't find any reason why the declaration using `interface` shouldn't work (versus `type`).

This issue might be related to https://github.com/facebook/flow/issues/5759 where the `interface` keyword acts differently to the `type` keyword.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.