Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of error TS2584 (TS) .... Try changing the 'lib' compiler option to include 'dom'?

I use VS and write Typescript

I would like simply to type the command "document.write" without having an error code. what I having now is an error code of this commmand "document". I just don't want to get into something complecated as there can be a simple solution ( I didn't experience this problem on my previous PC and VS).

Error message: Error TS2584 (TS) Cannot find name 'document'. Do you need to change your target library? Try changing the lib compiler option to include 'dom'. C:\web development\studies\typescript\replaceCharsInArray\replaceCharsInArray (tsconfig or jsconfig project) C:\web development\studies\typescript\replaceCharsInArray\replaceCharsInArray\app.ts 9 Active

let arr: string[] = ['black ofram','american walnut'];
function replaceCharsInStringArray(arr: string[], oldChar:string, newChar:string): string[] {
    for (let i = 0; i >= arr.length - 1, i++;) {
        arr[i] = arr[i].replace(newChar, oldChar);
        return arr;
    };
}
arr = replaceCharsInStringArray(arr, ' ', '_');
document.write

like image 705
Uri Gross Avatar asked Feb 23 '20 14:02

Uri Gross


2 Answers

As answered to this question. It seems that the problem is caused by targeting ES2016. Are you targeting that for a reason? If you target es6 the error will probably go away.

Another option is to specify the libraries for the compiler to use:

tsc -t ES2016 --lib "ES2016","DOM" ./your_file.ts

Which should also make the error go away.

I'm not sure why the libs aren't used by default, in the docs for compiler options it states for the --lib option:

Note: If --lib is not specified a default library is injected. The default library injected is:
► For --target ES5: DOM,ES5,ScriptHost
► For --target ES6: DOM,ES6,DOM.Iterable,ScriptHost

But it doesn't state what are the default libraries when targeting ES2016. It might be a bug, try to open an issue, if you do please share the link here.

like image 137
Husniddin Qurbonboyev Avatar answered Oct 23 '22 05:10

Husniddin Qurbonboyev


Adding "DOM" to list of lib in compilerOptions property to my tsconfig.json worked:

{

  "compilerOptions": {
      lib: ["ES2017", "DOM"]
  }
}

Documentation for lib

like image 37
Aakash Avatar answered Oct 23 '22 04:10

Aakash