Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ts] Module 'xxx' has no default export. #19

Closed
xcatliu opened this issue Jan 5, 2018 · 4 comments
Closed

[ts] Module 'xxx' has no default export. #19

xcatliu opened this issue Jan 5, 2018 · 4 comments

Comments

@xcatliu
Copy link
Owner

xcatliu commented Jan 5, 2018

为什么引用一些模块的时候,会报如下错误?

import fs from 'fs';
// [ts] Module '"fs"' has no default export.
@xcatliu
Copy link
Owner Author

xcatliu commented Jan 5, 2018

因为有的模块没有 defaultexport,需要这么引用:

import * as fs from 'fs';

@xcatliu
Copy link
Owner Author

xcatliu commented Jan 5, 2018

自己写的模块也会存在这个问题:

// foo.ts
const foo1 = 'foo1';
const foo2 = 'foo2';
export {
    foo1,
    foo2
}

// bar.ts
import foo from './foo';
// [ts] Module '"./foo"' has no default export.

必须得这么写才行:

// foo.ts
const foo1 = 'foo1';
const foo2 = 'foo2';
export {
    foo1,
    foo2
}

// bar.ts
import * as foo from './foo';

或者将 foo.ts 改写成 export default:

// foo.ts
const foo1 = 'foo1';
const foo2 = 'foo2';
export default {
    foo1,
    foo2
}

// bar.ts
import foo from './foo';

@xcatliu
Copy link
Owner Author

xcatliu commented Jan 5, 2018

另外,可以在 tsconfig.json 中开启配置允许这种写法:

{
    "compilerOptions": {
        "allowSyntheticDefaultImports": true
    }
}

这样即使没有 * as 也不会报错了。

参考:microsoft/TypeScript#3337

@xuqinggang
Copy link

楼上正解

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants