Skip to content

Problem with the type declaration of the first parameter of the decode method in index.d.ts? #294

@zurmokeeper

Description

@zurmokeeper

Problem with the type declaration of the first parameter of the decode method in index.d.ts?

export function decode(buffer: Buffer, encoding: string, options?: Options): string;

The first parameter of decode is declared as Buffer , but it is also possible to pass in string and number, but the correct result is not obtained. This will mislead developers, especially when the string type is used, the implementation code is as follows:

iconv.decode = function decode(buf, encoding, options) {
    if (typeof buf === 'string') {
        if (!iconv.skipDecodeWarning) {
        ......
        iconv.skipDecodeWarning = true;
    }

    buf = Buffer.from("" + (buf || ""), "binary"); // Ensure buffer.
    }
    ..........................................
}

The string is processed by default binary-encoded Buffer, and there is no description in the declaration file. I hope you can modify the declaration file, for example, change it to:

/**
 * @description [Buffer | string] content If the incoming string type, it will be processed with binary encoded Buffer
 */
 export function decode(content: Buffer | string, encoding: string, options?: Options): string;

In this way, the developer will clearly know what type of parameters to pass, and also know what the decode method does by default.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions