Object name not found in Typescript but is in Javascript

Viewed 110

I am using config-webpack to manage configuration settings in my application. https://www.npmjs.com/package/config-webpack

This takes all the configs in a JSON file and makes them available in the application as a JS object - i.e. CONFIG.SOMEBOOLEANSETTNIG

In my project, if I have a .js file this works fine and the following statement resolves;

if (CONFIG.SOMEBOOLEANSETTNIG==true) { ... }

However, the same code in a .ts or .tsx file displays an error with the CONFIG underlined with "Cannot find name 'CONFIG' ts(2304)" and the compilation fails.

I feel that I am missing something really simple but I am new to TS! Thanks.

I've tried looking into Typescript syntax documentation but I can't see where I am going wrong, so any help would be appreciated.

1 Answers

Consider declaring CONFIG as a global namespace for your local typescript.

Should look something like this:

import ConfigWebpackPlugin from 'config-webpack';

export default declare global {
  CONFIG: ConfigWebpackPlugin
}

NOTE: If you don't have a global type file you can make a custom one and add it to the typeRoots array in your tsconfig.json file.

"typeRoots": ["path/to/custom-type.d.ts" ]
Related