Skip to content

ArrayBuffer is not eliminated from union as expected in narrowing of ArrayBuffer | Uint8ArrayΒ #50714

@Jamesernator

Description

@Jamesernator

Bug Report

πŸ”Ž Search Terms

arraybuffer, narrow, union

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about ArrayBuffer

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

import fs from "node:fs/promises";

async function writeData(data: Uint8Array | ArrayBuffer): Promise<void> {
    if (data instanceof ArrayBuffer) {
        data = new Uint8Array(data);
    }
    // Argument of type 'Uint8Array | ArrayBuffer' is not assignable to parameter of type 'string | ArrayBufferView | Iterable<string | ArrayBufferView> | AsyncIterable<string | ArrayBufferView> | Stream'.
    //   Type 'ArrayBuffer' is not assignable to type 'string | ArrayBufferView | Iterable<string | ArrayBufferView> | AsyncIterable<string | ArrayBufferView> | Stream'.
    //      Type 'ArrayBuffer' is missing the following properties from type 'Float64Array': BYTES_PER_ELEMENT, buffer, byteOffset, copyWithin, and 24 more.(2345)
    await fs.writeFile("./some-file", data);
}

πŸ™ Actual behavior

Currently ArrayBuffer is not eliminated from the union, so data being passed to fs.writeFile is not of the right type.

πŸ™‚ Expected behavior

I expected the conditional to remove ArrayBuffer from the union, making data's type Uint8Array (after the conditional).

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions