“ ... 解析为非模块实体并且不能使用此构造导入”是什么意思?

我有一些 TypeScript 文件:

MyClass.ts

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

MyFuncts

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

我的消费者

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

在尝试使用 new时,这会给我带来错误

模块“ MyClass”解析为非模块实体,不能使用此构造导入。

当试图调用 fn()

无法调用其类型缺乏调用签名的表达式。

怎么了?

43310 次浏览

Why it doesn't work

import * as MC from './MyClass';

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.

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

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.

What does work?

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

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

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

MyClass.ts

export default class MyClass {
constructor() {
}
}

MyConsumer.ts

import MC from './MyClass';

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 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.

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.

I want to take advantage of my non-standard module loader

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.

Change MyConsumer.ts to:

import A from './a';

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

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.

Adding my 2 cents here incase someone else have this issue.

My way of working around the issue without modifying tsconfig.json (which can be problematic in some projects), I've gone with simply disabling the rule for oneline.

import MC = require('./MyClass'); // tslint:disable-line

I got this error when trying to include a npm debounce package in my project.

When I tried the accepted solution above I got an exception:

Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.

This ended up working:

import debounce from 'debounce'

TypeScript 2.7 introduces support by emitting new helper methods: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#support-for-import-d-from-cjs-form-commonjs-modules-with---esmoduleinterop

So in tsconfig.json add these two settings:

{
// Enable support for importing CommonJS modules targeting es6 modules
"esModuleInterop": true,


// When using above interop will get missing default export error from type check since
// modules use "export =" instead of "export default", enable this to ignore errors.
"allowSyntheticDefaultImports": true
}

And now you can use:

import MyClass from './MyClass';