错误 TS4058:导出函数的返回类型具有或正在使用来自外部模块 Y 的名称 X 但无法命名 [英] error TS4058: Return type of exported function has or is using name X from external module Y but cannot be named

查看:43
本文介绍了错误 TS4058:导出函数的返回类型具有或正在使用来自外部模块 Y 的名称 X 但无法命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 tsc v2.2.2

Using tsc v2.2.2

如何修复打字稿编译器错误:

How to fix typescript compiler error:

错误 TS4058:导出函数的返回类型具有或正在使用名称来自外部模块的{SomeInterface}"{某些路径}/dist/types"但不能命名.

error TS4058: Return type of exported function has or is using name '{SomeInterface}' from external module "{some path}/dist/types" but cannot be named.

我有index.tssomething.ts

// index.ts
import something from './something'

// error will point on this export below
export default function () {
   return {
     resultFunctionFrom: something()
   };
}


// something.ts
import {ICoolInterface} from 'some-module'

export default function () {
  return function (rootOfEvil:ICoolInterface) {
     // ...
  };
}

我会用这样的代码得到这个错误:

I will get this error with such code:

错误 TS4058:导出函数的返回类型具有或正在使用名称来自外部模块的ICoolInterface"/folder/node_modules/some-module/dist/types"但不能命名.

error TS4058: Return type of exported function has or is using name 'ICoolInterface' from external module "/folder/node_modules/some-module/dist/types" but cannot be named.

推荐答案

更新: 这在 TypeScript 2.9 中不应再发生,并解决了下面链接的问题 9944.https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#relaxing-declaration-emit-visiblity-rules

Update: This should no longer happen in TypeScript 2.9 and resolves Issue 9944 linked below. https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#relaxing-declaration-emit-visiblity-rules

目前需要在index.ts中显式导入ICoolInterface:

Currently you need to explicitly import ICoolInterface in index.ts:

// index.ts
import {ICoolInterface} from 'some-module'

考虑跟踪这个 GitHub 问题,他们正在考虑更改此 TypeScript 行为.

Consider tracking this GitHub issue where they are considering changing this TypeScript behaviour.

函数或变量上没有明确的类型注释.声明发射器推断它们的类型并尝试编写它.如果类型来自不同的模块,然后 a.它需要添加一个进口或 B.错误.

There is no explicit type annotation on the functions or variables. the declaration emitter infers their type and tries to write it. if the type is coming from a different module, then a. it needs to add an import or b. error.

发射器可以编写额外的导入,但这本来是以您未在您的代码.所以我们选择了错误.

The emitter can write the additional import, but that would have been changing your API shape in a way you did not indicate clearly in your code. so we opted to error instead.

修复方法是在源代码上添加显式类型注释问题.

the fix would be to add an explicit type annotation on the source of the problem.

有了 siad,我认为我们应该重新考虑这个设计决定,并添加导入.

Having siad that, i think we should reconsider this design decision, and add imports anyways.

注意:如果您使用的是 WebStorm,您将收到有关未使用的导入的警告.您可以使用导入上方的注释 //noinspection ES6UnusedImports 禁用警告.GUI 替代方法:在带有警告的导入行上按 Alt + Enter.Remove used 'import' 弹出菜单上的更多选项的向右箭头,然后选择 Suppress for statement 以禁用此特定行上的警告.

Note: if you are using WebStorm, you will be warned about an unused import. You can disable the warning with the comment //noinspection ES6UnusedImports above the import. GUI alternative: press Alt + Enter on the import line with the warning. Right arrow for more options on Remove unused 'import' popup menu and choose Suppress for statement to disable the warning on this particular line.

这篇关于错误 TS4058:导出函数的返回类型具有或正在使用来自外部模块 Y 的名称 X 但无法命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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