11

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

Uri Gross
  • 484
  • 1
  • 8
  • 20
  • 1
    I googled the error message and this looks promising: https://github.com/Microsoft/TypeScript/issues/28858#issuecomment-487756300 - updating the tsconfig.js compilerOptions like they say could fix you issue. – Andreas Dolk Feb 23 '20 at 14:40

3 Answers3

8

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.

7

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

{

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

Documentation for lib

Aakash
  • 21,375
  • 7
  • 100
  • 81
0

I would like to thank you all for helping me out.

I found another simple solution for my problem. I realized that I was creating a wrong solution type for my project. I added this extension: "Typescript Html application Template" and used it for TS project.

Uri Gross
  • 484
  • 1
  • 8
  • 20