TypeScript 在导入默认值时出现"Cannot find name"错误

owfi6suc  于 7个月前  发布在  TypeScript
关注(0)|答案(1)|浏览(84)

TypeScript版本: 3.0.1
搜索词: 找不到名称导入默认值
代码

// fetch.d.ts
declare function fetch(url: string): Promise<any>;
export = fetch;
declare namespace fetch {
  export { fetch as default };
}

// index.ts
import f from "./fetch";
f("url"); // error TS2304: Cannot find name 'f'

预期行为: 应该找到名称
实际行为: 找不到名称。有趣的是,import { default as f } from "./fetch";可以正常工作。
** playground链接:** (它不支持模块:/)
相关问题: 没有找到一个

sf6xfgos

sf6xfgos1#

我也遇到了从js文件中默认导入的问题,exports.default = Componentimport Component from '...'2.8.4版本中运行正常,现在只在import {default as Component} from '...'版本中运行。
我的tsconfig中有"allowSyntheticDefaultImports": true,将设置选项更改为false可以解决这个问题,但会在我的项目中带来更多的麻烦。
这可能是相关问题。

相关问题