Skip to content

Interface extending Array loses all type safety #5772

@massimonewsuk

Description

@massimonewsuk
interface NumberArray extends Array<number> { }

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<any>) {
}

If I use type NumberArray = Array<number>; 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 #5759 where the interface keyword acts differently to the type keyword.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions