简体   繁体   English

我可以缩小在TypeScript中使用require导入的模块的类型吗?

[英]Can I narrow the type of module imported using require in TypeScript?

Using the TypeScript 2.7.1 language service in VS Code, I can get hinting for a module imported into a JavaScript file as: 使用VS Code中的TypeScript 2.7.1语言服务,我可以得到有关导入到JavaScript文件中的模块的提示,如下所示:

const aModule = require('./aModule')

However, the type inference from the module includes the generic [x: string]: any; 但是,从模块进行的类型推断包括泛型[x: string]: any;

Which means that I can get autocomplete for valid exports from the module, but cannot get a warning if I use: 这意味着我可以自动完成从模块的有效导出,但是如果使用以下命令则不会收到警告:

aModule.something_that_doesnt_exist()

Is there a way to specify that the object is not extendable? 有没有一种方法可以指定对象是不可扩展的?

试试这个语法:

import * as aModule from './aModule';

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

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