不能调用或构造名称空间样式的导入,并且将在运行时导致失败 [英] A namespace-style import cannot be called or constructed, and will cause a failure at runtime
问题描述
在这里,发生奇怪的事情,使用TypeScript 2.7.2,在VSCode版本1.21中带有@ types/express及其后继的代码,在某些情况下,VSCode引发错误,指出无法调用或构造名称空间样式的导入,并且将在运行时导致失败.".但是,在具有类似设置和类似tsconfig.json文件的其他计算机上,代码也可以正常工作.
Strange stuff happening here, with TypeScript 2.7.2, in VSCode version 1.21 with @types/express and the code that follows, in some cases VSCode throws errors stating that "A namespace-style import cannot be called or constructed, and will cause a failure at runtime.". However on other machines with similar setups and similar tsconfig.json files the code just works.. What is happening here:
import { Bank } from './Bank';
import * as Express from 'express'; <== errors here..
let app: Express.Express;
this.app = Express(); <== and here
为什么会这样?
TIA,
约翰.
推荐答案
该错误仅在esModuleInterop: true
时发生.也许VSCode忽略了您的tsconfig.json
或使用了另一个esModuleInterop: true
的.
The error should only happen with esModuleInterop: true
. Maybe VSCode is ignoring your tsconfig.json
or another one with esModuleInterop: true
is used.
要进行最终修复,请将编译器选项esModuleInterop
设置为true
并使用import express
而不是import * as express
.
For a definitive fix set the compiler option esModuleInterop
to true
and use import express
instead of import * as express
.
问题:
ES6规范定义了命名空间对象"的概念.在此语句中,名称空间对象为namespaceObject
:import * as namespaceObject from 'a-module'
. typeof
这个对象是object
,您不应调用它.
The ES6 specification defines the notion of "namespace object". A namespace object is namespaceObject
in this statement : import * as namespaceObject from 'a-module'
. The typeof
this object is object
, you are not supposed to be able to call it.
到目前为止,您一直在使用import * as express
,因为express
是用于Node.js的CommonJS模块,它是使用module.exports
导出的.但是,这在ES6规范中是非法的,TypeScript现在会警告您.
You were using import * as express
until now because express
is a CommonJS module, for Node.js, it is exported using module.exports
. However it is illegal in the ES6 spec, and TypeScript now warns you.
解决方案:
将esModuleInterop
设置为true将使TypeScript包装您的import
调用,以检查该模块是ES6模块还是CommonJS.如果它是CommonJS模块,而您正在使用import default from 'module'
,那么TypeScript将找出并返回正确的CommonJS模块.
Setting esModuleInterop
to true will make TypeScript wrap your import
calls to check if the module is an ES6 module or a CommonJS. If it's a CommonJS module and you are using import default from 'module'
TypeScript will find out and return the correct CommonJS module.
从 TypeScript发行说明中:
注意:新行为将添加到标志下,以避免不必要的行为 破坏现有的代码库.我们强烈建议将两者都应用于 新的和现有的项目.对于现有项目,名称空间导入 (从* express中导入*为express; express();)必须为 转换为默认导入(来自"express"的导入快递; express();).
Note: The new behavior is added under a flag to avoid unwarranted breaks to existing code bases. We highly recommend applying it both to new and existing projects. For existing projects, namespace imports (import * as express from "express"; express();) will need to be converted to default imports (import express from "express"; express();).
这篇关于不能调用或构造名称空间样式的导入,并且将在运行时导致失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!