1

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.

Jaycee26
  • 11
  • 1
  • 3

1 Answers1

0

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" ]
fortunee
  • 3,852
  • 2
  • 17
  • 29
  • Thank you. I tried the suggestion but get "Cannot find name 'declare'" and under global get "';' expected" and "error TS2670: Augmentations for the global scope should have 'declare' modifier unless they appear in already ambient context." – Jaycee26 Mar 25 '22 at 09:17
  • @Jaycee26 What happens when you try installing the types for this package by running `npm install -D @types/config-webpack`? – fortunee Mar 25 '22 at 16:33
  • I did try that and '@types/config-webpack is not in this registry' – Jaycee26 Mar 25 '22 at 20:29
  • Try [this](https://stackoverflow.com/a/59499895/5589405) – fortunee Mar 25 '22 at 23:46