无法使用打字稿将选项对象添加到 SHA3 函数

Can't add option object to the SHA3 function with typescript

SHA3 函数提供了一个选项来控制输出长度,如本例所示:

var hash = CryptoJS.SHA3("Message", { outputLength: 512 });
var hash = CryptoJS.SHA3("Message", { outputLength: 384 });
var hash = CryptoJS.SHA3("Message", { outputLength: 256 });
var hash = CryptoJS.SHA3("Message", { outputLength: 224 });

然而,当在带有 @types/crypto-js 的打字稿中使用时,我在尝试指定 outputLength:

时收到以下类型错误

Argument of type '{ outputLength: number; }' is not assignable to parameter of type 'string | WordArray | undefined'. Object literal may only specify known properties, and 'outputLength' does not exist in type 'string | WordArray | undefined'.

那是因为该函数在 @types/crypto-js 中定义为:

(message: string | LibWordArray, key?: string | WordArray, ...options: any[]) => WordArray;

如您所见,它需要三个参数,第二个是键(可为空)。只需这样做:

CryptoJS.SHA3("Message", undefined, { outputLength: 512});

它应该可以工作。

顺便说一句,这个库还有另一个@types,@types\cryptojs,它可能更适合你。