Skip to content

Typescript issue: Type is not assignable to type 'Encrypter'. #19

Answered by niketpathak
NayamAmarshe asked this question in Q&A
Discussion options

You must be logged in to vote

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.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@NayamAmarshe
Comment options

@niketpathak
Comment options

Answer selected by niketpathak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants