0

My TypeScript project is compiled for the browser with AMD modules. I include lodash.min.js myself, along with a bunch of other UMD libraries that all declare global variables, like moment and _. I need to tell my TypeScript about the global _ variable for Lodash. How can I do this with imports? If I install @types/lodash and use import * as _ from "lodash";, it tries to dynamically fetch an AMD Lodash module at runtime, which breaks everything.

If I have one file where I declare the _ global, i.e. export declare const _: LodashStatic;, I cannot even import my declaration in another file, or it tries to load stuff at runtime. What is even the point of the declare keyword if TypeScript treats it like a functioning import. I thought declarations just tell the compiler to shut up and that there will be separately loaded stuff at runtime. Lucky for me, I can use declare const _: LodashStatic; in every single one of my hundreds of files and everything works, but it would be nice if I could just declare it once and then import in other files...

Sam Claus
  • 1,807
  • 23
  • 39

1 Answers1

-1

The usual way to tell Typescript about the existence of global types is through the types array in your tsconfig.json. If you have @types/lodash installed, then having the following in your tsconfig.json should allow you to reference _ anywhere:

{
  "compilerOptions": {
    "types": ["lodash"]
  }
}
ethan.roday
  • 2,485
  • 1
  • 23
  • 27