I am currently working on adding specific types for the config
module in our application. The config
module is generated dynamically from a JSON file, making it challenging to type. Since it is a node module, I am utilizing an ambient module for the typings.
// config.d.ts
declare module 'config' {
interface AppConfig {
name: string;
app_specific_thing: string;
}
const config: AppConfig;
export = config;
}
My goal is to also export AppConfig
so that I can use it as a type like this:
import * as config from 'config';
const appConfig: config.AppConfig;
My Attempts
When attempting to export AppConfig directly within the
config
module, I encountered this error:TS2309: An export assignment cannot be used in a module with other exported elements.
Moving
AppConfig
to another file (e.g../app_config
) and importing it intoconfig.d.ts
resulted in this error:TS2439: Import or export declaration in an ambient module declaration cannot reference module through relative module name.
Placing the
AppConfig
export outside of theconfig
module in the same file led to this error:TS2665: Invalid module name in augmentation. Module 'config' resolves to an untyped module at $PROJ/config/lib/config.js, which cannot be augmented.
This issue resembles a situation discussed in this Stackoverflow thread, where the main requirement is to have the ability to import AppConfig
as a type directly in other TypeScript files.