question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ashtuchkincommented, Mar 7, 2022

Unfortunately in JS you can pass anything anywhere. It’s not guaranteed it will work, especially if it’s not supported by the typescript declaration. The fact that it somewhat works today is an artifact of the past and should not be relied upon. Please don’t pass strings to decode method. See https://github.com/ashtuchkin/iconv-lite/wiki/Use-Buffers-when-decoding for details.

0reactions
ashtuchkincommented, Mar 7, 2022

This will unfortunately break legacy code that is passing strings.

Read more comments on GitHub >

github_iconTop Results From Across the Web

types/jest index.d.ts file returning error - Stack Overflow
Here are the errors it's returning when I go to npm start my project: [ ERROR ] TypeScript: node_modules/@types/jest/index. d. ts:39:30 A rest...
Read more >
Types declared in types.d.ts not included in output of tsc ...
The command tsc -p jsconfig.json --noEmit false --declaration --emitDeclarationOnly --outDir types generates this types/index.d.ts : /** @ ...
Read more >
The arguments object - JavaScript - MDN Web Docs - Mozilla
arguments is an Array-like object accessible inside functions that contains the values of the arguments passed to that function.
Read more >
protobuf.js
Types marked as optimal provide the best performance because no conversion step (i.e. number to low and high bits or base64 string to...
Read more >
Database Engine events and errors - SQL Server
Consult this MSSQL error code list to find explanations for error messages ... 189, 15, No, The %ls function requires %d to %d...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found