简体   繁体   English

在扩展对象上获取TypeScript代码完成

[英]Get TypeScript code completion on extended objects

I have a config file for my app conf.ts , which merges config values from other files, to keep it organized. 我为我的应用程序conf.ts了一个配置文件,该文件合并了其他文件中的配置值,以保持其组织性。 I merge them, because when I want to use a config value, it is easier to write Conf.MY_LONG_NAMED_VALUE , than Conf.SubCategory.MY_LONG_NAMED_VALUE 我合并它们,因为当我想使用配置值时,写Conf.MY_LONG_NAMED_VALUEConf.SubCategory.MY_LONG_NAMED_VALUE

import {CoreConf} from './conf/game/core.conf';
import {ViewConf} from './conf/view/view.conf';
import {DebugConf} from './conf/game/debug.conf';

/**
 *
 * @namespace Conf
 */
export const Conf: {[index: string] : any} = $.extend({},
    CoreConf,
    DebugConf,
    ViewConf
);

I am currently migrating from JavaScript to TypeScript, and while JavaScript code completion in WebStorm worked (because of the JSDoc tag @namespace ), TypeScript does not autocomplete the config names in the sub categories. 我目前正在从JavaScript迁移到TypeScript,并且虽然WebStorm中的JavaScript代码完成工作正常(由于JSDoc标签@namespace ),但TypeScript不会自动完成子类别中的配置名称。

The two solutions I've found, is to either just use one file = one object, which is less organized, or create an index signature (or interface) with all config names, which is double the work. 我发现的两个解决方案是要么仅使用一个文件=一个对象,但对象的组织性较差,要么使用所有配置名称创建索引签名(或接口),这是工作的两倍。

Is there a better way to make this work? 有没有更好的方法可以使这项工作?

You can type Conf to be an intersection type between your configs: 您可以输入Conf作为配置之间的交集类型:

export const Conf: typeof CoreConf & typeof DebugConf & typeof ViewConf 

Or you could use Object.assign which is typed to return an intersection type of all parameter types 或者,您可以使用Object.assign键入返回所有参数类型的交集类型

export const Conf = Object.assign({}, CoreConf, DebugConf, ViewConf)

While researching a different problem, I stumbled upon a third solution - spread syntax: 在研究另一个问题时,我偶然发现了第三个解决方案-传播语法:

export const Conf = {...CoreConf, ...DebugConf, ...ViewConf}

For me this benefitted by forcing an Excess Property Check, which I wanted in another part of my code. 对我来说,这通过强制执行“多余的属性检查”而受益,这是我在代码的另一部分中想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM