Typescript issue: Type is not assignable to type 'Encrypter'. #19
-
I have this snippet: // LOCALSTORAGE-SLIM
ls.config.encrypter = (data: unknown, secret: string): string => {
return CryptoJS.AES.encrypt(data, secret).toString();
};
ls.config.decrypter = (encryptedString, secret) => {
const bytes = CryptoJS.AES.decrypt(encryptedString, secret);
return bytes.toString(CryptoJS.enc.Utf8);
}; but typescript throws an error: How can I fix this? Tried a few things but they don't seem to work. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hello @NayamAmarshe , You need to use type-assertion since localstorage-slim does not have an idea about what you plan to use internally within the Encrypter/Decrypter functions (i.e. CryptoJS or some other encryption library) If you try this, your compiler will not complain -> ls.config.encrypter = (data, secret): string => {
return CryptoJS.AES.encrypt((data as string).toString(), secret as string).toString();
};
ls.config.decrypter = (encryptedString, secret) => {
const bytes = CryptoJS.AES.decrypt(encryptedString as string, secret as string);
return bytes.toString(CryptoJS.enc.Utf8);
}; Here is a simple article on type-assertion to clarify your doubts. |
Beta Was this translation helpful? Give feedback.
Hello @NayamAmarshe ,
You need to use type-assertion since localstorage-slim does not have an idea about what you plan to use internally within the Encrypter/Decrypter functions (i.e. CryptoJS or some other encryption library)
If you try this, your compiler will not complain ->
Here is a simple article on type-assertion to clarify your doubts.