“...解析为非模块实体并且无法使用此构造导入"是什么意思?意思是? [英] What does "... resolves to a non-module entity and cannot be imported using this construct" mean?

查看:17
本文介绍了“...解析为非模块实体并且无法使用此构造导入"是什么意思?意思是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 TypeScript 文件:

I have some TypeScript files:

MyClass.ts

class MyClass {
  constructor() {
  }
}
export = MyClass;

MyFunc.ts

function fn() { return 0; }
export = fn;

MyConsumer.ts

import * as MC from './MyClass';
import * as fn from './MyFunc';
fn();

这在尝试使用 new

模块MyClass"解析为非模块实体,无法使用此构造导入.

Module "MyClass" resolves to a non-module entity and cannot be imported using this construct.

并且在尝试调用 fn()

无法调用类型缺少调用签名的表达式.

Cannot invoke an expression whose type lacks a call signature.

是什么?

推荐答案

为什么它不起作用

import * as MC from './MyClass';

这是 ES6/ES2015 风格的 import 语法.其确切含义是获取从 ./MyClass 加载的模块 namespace object 并在本地将其用作 MC".值得注意的是,模块命名空间对象"仅由具有属性的普通对象组成.ES6 模块对象不能作为函数或使用 new 调用.

This is ES6/ES2015-style import syntax. The exact meaning of this is "Take the module namespace object loaded from ./MyClass and use it locally as MC". Notably, the "module namespace object" consists only of a plain object with properties. An ES6 module object cannot be invoked as a function or with new.

再说一遍:ES6 模块命名空间对象不能作为函数或使用 new 调用.

To say it again: An ES6 module namespace object cannot be invoked as a function or with new.

您从模块中使用 * as X import 的东西被定义为只有属性.在较低级别的 CommonJS 中,这可能不会得到完全尊重,但 TypeScript 会告诉您标准定义的行为是什么.

The thing you import using * as X from a module is defined to only have properties. In downleveled CommonJS this might not be fully respected, but TypeScript is telling you what the behavior defined by the standard is.

你需要使用 CommonJS 风格的导入语法来使用这个模块:

You'll need to use the CommonJS-style import syntax to use this module:

import MC = require('./MyClass');

如果你控制两个模块,你可以使用export default代替:

If you control both modules, you can use export default instead:

MyClass.ts

export default class MyClass {
  constructor() {
  }
}

MyConsumer.ts

import MC from './MyClass';

我很伤心;规则是愚蠢的.

使用 ES6 导入语法会很好,但现在我必须这样做 import MC = require('./MyClass'); 事情?2013年就是这样!瘸!但悲伤是编程的正常部分.请跳到库伯勒-罗斯模型的第五阶段:接受.

I'm Sad About This; Rules are Dumb.

It would have been nice to use ES6 import syntax, but now I have to do this import MC = require('./MyClass'); thing? It's so 2013! Lame! But grief is a normal part of programming. Please jump to stage five in the Kübler-Ross model: Acceptance.

TypeScript 在这里告诉你这行不通,因为它行不通.有一些技巧(将 namespace 声明添加到 MyClass 是一种流行的假装它有效的方法),它们可能今天在您的特定降级中有效模块捆绑器(例如汇总),但这是虚幻的.目前还没有任何 ES6 模块实现,但不会永远如此.

TypeScript here is telling you this doesn't work, because it doesn't work. There are hacks (adding a namespace declaration to MyClass is a popular way to pretend this works), and they might work today in your particular downleveling module bundler (e.g. rollup), but this is illusory. There aren't any ES6 module implementations in the wild yet, but that won't be true forever.

想象一下你未来的自己,尝试在一个neato原生ES6模块实现上运行,并发现你已经为自己的重大失败做好了准备,因为尝试使用ES6语法来做一些ES6明确没有做的事情.

Picture your future self, trying to run on a neato native ES6 module implementation and finding that you've set yourself up for major failure by trying to use ES6 syntax to do something that ES6 explicitly doesn't do.

也许你有一个模块加载器,它在不存在时帮助"创建default 导出.我的意思是,人们制定标准是有原因的,但有时忽略标准很有趣,我们可以认为这是一件很酷的事情.

Maybe you have a module loader that "helpfully" creates default exports when none exist. I mean, people make standards for a reason, but ignoring standards is fun sometimes and we can think that's a cool thing to do.

MyConsumer.ts 更改为:

import A from './a';

并指定 allowSyntheticDefaultImports 命令行或 tsconfig.json 选项.

And specify the allowSyntheticDefaultImports command-line or tsconfig.json option.

请注意,allowSyntheticDefaultImports 根本不会改变代码的运行时行为.它只是一个标志,告诉 TypeScript 你的模块加载器在不存在时创建 default 导出.它不会神奇地让您的代码在 nodejs 中工作,而之前没有.

Note that allowSyntheticDefaultImports doesn't change the runtime behavior of your code at all. It's just a flag that tells TypeScript that your module loader creates default exports when none exist. It won't magically make your code work in nodejs when it didn't before.

这篇关于“...解析为非模块实体并且无法使用此构造导入"是什么意思?意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆