LoginSignup
0
0

The signature '(data: string): string' of 'btoa' is deprecated.ts(6387) が出たときの解決方法

Last updated at Posted at 2024-01-28

はじめに

以下のように Base64 でエンコード・デコード処理を実装しようとした際、TypeScript の非推奨エラーがでたので、その解決方法のメモです。

index.ts
const encoded = btoa("1234");
console.log(encoded);
const decoded = atob(encoded);
console.log(decoded);

エラーメッセージ

  • The signature '(data: string): string' of 'btoa' is deprecated.ts(6387)
  • The signature '(data: string): string' of 'atob' is deprecated.ts(6387)

2024年1月時点で、MDN のサンプル通りの書き方をしていたので、一瞬何でだろうと思いました(サンプルは、JS ですが)。

const encodedData = btoa("Hello, world"); // 文字列をエンコード
const decodedData = atob(encodedData); // 文字列をデコード

解決方法

各 API の前に window を追加すれば、解決できました。

index.ts
const encoded = window.btoa("1234");
console.log(encoded);
const decoded = window.atob(encoded);
console.log(decoded);

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0