Search code examples
typescriptwebstorm

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"
    ]
  }
}

Solution

  • 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.