Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TS2304:Cannot find name 'btoa'

I'm using btoa method for hashing username, password which seems to work fine but this error shows up in my TypeScript errors - WebStorm. How to resolve this?

js code

let base64hash = btoa(user.username+ ':' + user.key);

tsconfig.json

/* tsconfig.json */
{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "es2016"
    ]
  }
}
like image 862
sachq Avatar asked Mar 20 '17 08:03

sachq


1 Answers

The reason that the compiler can't find the definition for btoa() is that you're not including all the necessary libs.

When you use the --lib compiler option you need to include all the libs that are included by default when you're not using this option.
In your case what's missing is the DOM lib, so it should be:

/* tsconfig.json */
{
    ...
    "compilerOptions": {
        ...
        "lib": [
            "es2016", "DOM"
        ]
    }
}

As the definition for btoa is in the lib.dom.d.ts.

like image 68
Nitzan Tomer Avatar answered Oct 26 '22 05:10

Nitzan Tomer