Problem with the type declaration of the first parameter of the decode method in index.d.ts?
See original GitHub issueProblem 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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
This will unfortunately break legacy code that is passing strings.