24

安装node-config@types/config

yarn add config
yarn add --dev @types/config

并按照lorenwest/node-config中的描述添加配置:

// default.ts
export default {
  server: {
    port: 4000,
  },
  logLevel: 'error',
};

当我尝试在我的应用程序中使用时:

import config from 'config';

console.log(config.server);

我收到错误消息:

src/app.ts(19,53): error TS2339: Property 'server' does not exist on type 'IConfig'.
4

7 回答 7

12

我采用了一种稍微不同的方法——在 JavaScript 中定义变量,并在 TypeScript 中访问它们。

使用以下文件夹结构:

├── config
│   ├── custom-environment-variables.js
│   ├── default.js
│   ├── development.js
│   └── production.js
└── server
    ├── config.ts
    └── main.ts

我在根config/文件夹中定义配置。例如:

// config/default.js
module.exports = {
  cache: false,
  port: undefined  // Setting to undefined ensures the environment config must define it
};

// config/development.js
module.exports = {
  port: '3000'
}

// config/production.js
module.exports = {
  cache: true
}

// config/custom-environment-variables.js
module.exports = {
  port: 'PORT'
}

现在,在 TypeScript 领域,我定义了一个接口来提供更好的自动完成和文档,并编写一些桥接代码将配置从node-config我的配置映射中拉入:

// server/config.ts
import nodeConfig from 'config';

interface Config {
  /** Whether assets should be cached or not. */
  cache: boolean;

  /** The port that the express server should bind to. */
  port: string;
}

const config: Config = {
  cache: nodeConfig.get<boolean>('cache'),
  port: nodeConfig.get<string>('port')
};

export default config;

最后,我现在可以在任何 TypeScript 代码中导入和使用我的配置变量。

// server/main.ts
import express from 'express';
import config from './config';

const { port } = config;

const app = express();

app.listen(port);

这种方法有以下好处:

  • 我们可以使用丰富且经过实战考验的功能,node-config而无需重新发明轮子
  • 我们有一个强类型、有据可查的配置映射,可以从我们的 TS 代码中的任何地方导入和使用
于 2020-04-20T20:40:30.763 回答
11

config.get实用程序可用于获取配置值,如下所示:

import config from 'config';

const port: number = config.get('server.port');
于 2018-06-12T09:50:52.537 回答
6

使用这个“import * as config from 'config';” 而不是“从'config'导入配置;”

    import * as config from 'config';

    const port = config.get('server.port');
    console.log('port', port);
    // port 4000

配置/开发.json

    {
      "server": {
          "port": 4000
      }
    }

并设置 NODE_ENV=development

 export NODE_ENV=development

注意:如果您使用默认设置,则不需要设置此 NODE_ENV

于 2019-03-18T11:44:20.180 回答
5

从以前开始,我仍然遇到config无法serverdefault.ts.

下面是我如何使用 npm 配置模块。更新export default {export =

// default.ts
export = {
  server: {
    port: 4000,
  },
  logLevel: 'error',
};

应用程序内的用法[相同]:

import config from 'config';

console.log(config.get('server'));
于 2019-02-14T01:07:12.207 回答
1

我使用IConfig接口,所以我可以先设置配置路径:

import { IConfig } from 'config';

export function dosomething() {

  process.env["NODE_CONFIG_DIR"] = 'path to config dir';

  //using get
  const config: IConfig = require("config");
  const port = config.get('server.port');
  console.log('port', port);

  //using custom schema
  const config2: { server: { port: number } } = require("config");
  console.log('config2.server.port', config2.server.port);

}

//port 4000
//config2.server.port 4000
于 2019-01-08T01:20:33.693 回答
1

我可以完成这项工作的唯一方法是卸载@types/config和修改类型定义以包含我的配置文件。

config.d.ts

    declare module 'config' {
    
      // Importing my config files
      import dev from '#config/development.json'
      import test from '#config/test.json'
      import prod from '#config/production.json'
    
      // Creating a union of my config
      type Config = typeof dev | typeof test | typeof prod
    
      var c: c.IConfig;
    
      namespace c {
    
        // see https://github.com/lorenwest/node-config/wiki/Using-Config-Utilities
        interface IUtil {
            // Extend an object (and any object it contains) with one or more objects (and objects contained in them).
            extendDeep(mergeInto: any, mergeFrom: any, depth?: number): any;
    
            // Return a deep copy of the specified object.
            cloneDeep(copyFrom: any, depth?: number): any;
    
            // Return true if two objects have equal contents.
            equalsDeep(object1: any, object2: any, dept?: number): boolean;
    
            // Returns an object containing all elements that differ between two objects.
            diffDeep(object1: any, object2: any, depth?: number): any;
    
            // Make a javascript object property immutable (assuring it cannot be changed from the current value).
            makeImmutable(object: any, propertyName?: string, propertyValue?: string): any;
    
            // Make an object property hidden so it doesn't appear when enumerating elements of the object.
            makeHidden(object: any, propertyName: string, propertyValue?: string): any;
    
            // Get the current value of a config environment variable
            getEnv(varName: string): string;
    
            // Return the config for the project based on directory param if not directory then return default one (config).
            loadFileConfigs(configDir?: string): any;
    
            // Return the sources for the configurations
            getConfigSources(): IConfigSource[];
            
            // Returns a new deep copy of the current config object, or any part of the config if provided.
            toObject(config?: any): any;
    
            /**
             * This allows module developers to attach their configurations onto
             * the 6 years agoInitial 0.4 checkin default configuration object so
             * they can be configured by the consumers of the module.
             */
            setModuleDefaults(moduleName:string, defaults:any): any;
        }
    
        interface IConfig {
            // Changed the get method definition.
            get<K extends keyof Config>(setting: K): Config[K];
            has(setting: string): boolean;
            util: IUtil;
        }
    
        interface IConfigSource {
            name: string;
            original?: string;
            parsed: any;
        }
      }
    
      export = c;
    
    }

然后我可以做这样的事情:

在此处输入图像描述

于 2021-12-01T20:35:20.377 回答
-2

您可以使用any返回类型。

const serverConfig: any = config.get('server');
于 2021-04-17T18:35:50.533 回答