8000 Type error in Buffer.from() · Issue #23155 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content
Type error in Buffer.from() #23155
Closed
Closed
@JustinBeckwith

Description

@JustinBeckwith

Greetings folks! 👋
I'm getting a type error I can't explain. I have some code like this:

function doStuff(foo: string|Buffer) {
  return Buffer.from(foo);
}

This gives me the compilation error:

Argument of type 'string | Buffer' is not assignable to parameter of type 'string'. Type 'Buffer' is not assignable to type 'string'.

This is weird, because Buffer.from can accept a string or a Buffer. The d.ts for node.js appears to check out:

    /**
     * Copies the passed {buffer} data onto a new Buffer instance.
     */
    from(buffer: Buffer): Buffer;
    /**
     * Creates a new Buffer containing the given JavaScript string {str}.
     * If provided, the {encoding} parameter identifies the character encoding.
     * If not provided, {encoding} defaults to 'utf8'.
     */
    from(str: string, encoding?: string): Buffer;

This kinda feels like a bug. Am I missing something? Thanks!

cc @ofrobots

Metadata

Metadata

Assignees

No one assigned

    Labels

    ExternalRelates to another program, environment, or user action which we cannot control.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0