Typescript toString ('hex') doesn't work

Please help me convert buffer to hex in typescript. I am getting an error error TS2554: Expected 0 arguments, but got 1.

when I try to calldata.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 copied and pasted from the Node.js docs for cryptography

+3


source to share


1 answer


cipher.read()

returns string | Buffer

in which only the type Buffer

has an overload for toString

that takes a parameter.

You can try to assert what data

type is Buffer

:

encrypted += (data as Buffer).toString('hex');

      



Or you can use instanceof

the guard type :

if (data instanceof Buffer)
    encrypted += data.toString('hex'); // `data` is inferred as `Buffer` here

      

+3


source







All Articles