【问题标题】:Can't find export from module typescript无法从模块打字稿中找到导出
【发布时间】:2022-01-19 13:36:18
【问题描述】:

得到一个导出对象的文件:

let btypes:{[key:string]:any} = {
"key1":val,
//...
} 
export {btypes}

我也试过export default btypes

当我导入它时:

import {btypes} from "../types";

终端输出:

src/tests/parse.test.ts:3:8 - 错误 TS1192:模块 '"/abc/types"' 没有默认导出。

或者

src/tests/parse.test.ts:3:9 - 错误 TS2305:模块 '"../types"' 没有导出的成员 'btypes'。

知道要在这里寻找什么吗?

【问题讨论】:

    标签: typescript jestjs


    【解决方案1】:

    尝试像这样导出:

    export default {btypes};
    

    然后像这样导入:

    import * as btypes from '../types';
    

    【讨论】:

    【解决方案2】:

    他们说:in the playground(还有in the docs):

    (...) 默认导出有一个令人困惑的部分:/ 一些导出的文档暗示您可以像这样编写导入:import req from "request";

    但是失败了,然后你会发现堆栈溢出,建议导入为:import * as req from "request";

    这是@Mustafa 建议的默认导出

    还有另一个解决方案写在操场上。将此添加到 tsconfig.js

    1. esModuleInterop:true
    2. allowSyntheticDefaultImports:true

    现在import myDefaultExport from './myMod'; 应该足够了。

    【讨论】:

      猜你喜欢
      • 2017-01-12
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 2017-09-23
      • 2021-01-07
      • 2013-12-30
      相关资源
      最近更新 更多