TypeScript-使用toml解析后,对象上的属性不存在

问题描述

我的TypeScript项目是模块化的,并且具有多个配置文件。 我选择TOML作为配置文件,因为它是一种非常直观的语言。

此外,我得到了main.toml,您可以在其中启用/禁用模块。 我的配置类看起来像这样。它是用于从中创建多个可自动解析的配置的。

import { parse } from 'toml';
import { readFileSync } from 'fs';
import { join } from 'path';

export class Config {
    private readonly _name: string;
    private readonly _path: string;
    private _config: object;

    constructor(name: string) {
        this._name = name;
        this._path = join(__dirname,'..','config',`${name}.toml`);
        this._config = this.load();
    }

    public load(): object {
        let config: object = {};
        try {
            config = parse(readFileSync(this._path).toString());
        } catch (error) {
            if (error.code === 'ENOENT') {
                throw new Error(`config ${this._name}.toml was not found!`);
            } else {
                throw new Error(error);
            }
        }
        return config;
    }

    get config(): object {
        return this._config;
    }
}

这是我的主文件在我想使用main.toml激活其他模块的地方的样子:

import { Config } from './init/config';

let config: object = {};
try {
    config = new Config('main').config;
} catch (error) {
    console.log(error.stack);
    process.exit(1);
}

for (const key in config.modules) {
    if (Object.prototype.hasOwnProperty.call(config.modules,key) && config.modules[key]) {
        require(`./modules/${key}/${key}`);
    } else {
        zoya.info(`skipping module ${key}...`);
    }
}

现在我遇到的问题是,每次我使用config.modules时,打字稿编译器都会给我以下错误

TS2339: Property 'modules' does not exist on type 'object'.

我可以用@ts-ignore来抑制它,顺便说一句效果很好,但是我认为这是一种不好的做法,我想知道我是否可以以某种方式防止这种情况发生。

我还尝试了其他this这样的TOML解析器,希望能有所作为,但我遇到了完全相同的问题。

解决方法

Typescript无法推断解析后的配置的结构。请记住,Typescript在编译时存在,但在运行时不存在,而解析的配置对象在运行时但在编译时不存在。

您告诉Typescript您解析的配置的类型为object,但是object没有modules属性。

您在这里有两个选择:

  1. _config定义为any,而不是object。这将告诉Typescript该对象可以是任何类型,这意味着基本上不会对其进行类型检查。
  2. 为您的配置对象定义接口,因此Typescript知道应该从中获取什么类型。琐碎:
interface ConfigDef {
   modules: SomeType[]
}

let config: ConfigDef = { modules: [] };
try {
    config = new Config('main').config as ConfigDef;
} catch (error) {
    console.log(error.stack);
    process.exit(1);
}

或更严格地说,使用泛型(可能更好):

export class Config<T> {
    private readonly _name: string;
    private readonly _path: string;
    private _config: T;

    constructor(name: string) {
        this._name = name;
        this._path = join(__dirname,'..','config',`${name}.toml`);
        this._config = this.load();
    }

    public load(): T {
        let config: T = {};
        try {
            config = parse(readFileSync(this._path).toString()) as T;
        } catch (error) {
            if (error.code === 'ENOENT') {
                throw new Error(`config ${this._name}.toml was not found!`);
            } else {
                throw new Error(error);
            }
        }
        return config;
    }

    get config(): T {
        return this._config;
    }
}

// ...

let config: ConfigDef = { modules: [] };
try {
    config = new Config<ConfigDef>('main').config;
} catch (error) {
    console.log(error.stack);
    process.exit(1);
}