I'm trying to add types for the config
module specific to our app. The config
module is dynamically generated from a JSON file so it's tricky to type. Since it's a node module, I'm using 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;
}
How do I also export AppConfig
so I can use it as a type like so:
import * as config from 'config';
const appConfig: config.AppConfig;
Attempts
If I export AppConfig directly in the
config
module it errors with:TS2309: An export assignment cannot be used in a module with other exported elements.
If I move
AppConfig
to another file (e.g../app_config
) to hold the exports and import them intoconfig.d.ts
it errors with:TS2439: Import or export declaration in an ambient module declaration cannot reference module through relative module name.
If I put the
AppConfig
export in the same file, but outside theconfig
module, it errors with:TS2665: Invalid module name in augmentation. Module 'config' resolves to an untyped module at $PROJ/config/lib/config.js, which cannot be augmented.
This is similar to Typescript error "An export assignment cannot be used in a module with other exported elements." while extending typescript definitions with the requirement that I want to be able to import AppConfig
as a type directly in other TS files.
AppConfig
? I tried importing* as config
and usingtype AppConfig = typeof config
, which worked, so that's a workaround at least – michaeln