Please someone help me convert a buffer to hex in typescript. I'm getting the error error TS2554: Expected 0 arguments, but got 1.
when trying to call data.toString('hex')
const cipher = crypto.createCipher('aes192', config.secret);
let encrypted = '';
cipher.on('readable', () => {
const data = cipher.read();
if (data) {
encrypted += data.toString('hex');
}
});
cipher.on('end', () => {
secret = encrypted;
resolve(encrypted);
});
cipher.write('some clear text data');
cipher.end();
This code example is practically copy and pasted from Node.js docs for crypto
cipher.read()
returns string | Buffer
in which only the type Buffer
has the overload for toString
that accepts a parameter.
You can try asserting that data
is of type Buffer
:
encrypted += (data as Buffer).toString('hex');
Or you can use an instanceof
type guard:
if (data instanceof Buffer)
encrypted += data.toString('hex'); // `data` is inferred as `Buffer` here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With