typescript 当被两个不同的文件引用时,打字稿找不到外部模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28844917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Typescript can't find external module when referenced by two different files
提问by Mike.T
I may be missing something obvious, but Typescript's module resolver for commonjs isn't working as expected.
我可能遗漏了一些明显的东西,但是用于 commonjs 的 Typescript 模块解析器没有按预期工作。
Given the following directory structure and files:
给定以下目录结构和文件:
./first/shared.ts
./first/second/class_a.ts
./first/second/class_b.ts
./third/class_c.ts
Where:
在哪里:
- class_a references shared.ts
- class_b references class_a.ts and class_c.ts
- class_c references shared.ts
- class_a 引用了 shared.ts
- class_b 引用 class_a.ts 和 class_c.ts
- class_c 引用了 shared.ts
Specifically:
具体来说:
shared.ts:
class Shared{}
export = Shared;
class_a.ts:
import Shared = require('../shared');
class A{}
export = A;
class_b.ts:
import A = require('./class_a');
import C = require('../../third/class_c');
class B {}
export = B;
class_c.ts:
import Shared = require('../first/shared');
class C {}
export = C;
All compile except class_b.ts, using the following compiler invocation:
除了 class_b.ts 之外的所有编译,使用以下编译器调用:
tsc --module commonjs class_a.ts
Compiling class_b.ts fails with the error that it can't find shared.ts:
编译 class_b.ts 失败,错误是找不到 shared.ts:
../../third/class_c.ts(1,25): error TS2307: Cannot find external module '../first/shared'.
Note that if you reverse the order of imports in class_b.ts, you get a different error:
请注意,如果您颠倒 class_b.ts 中的导入顺序,则会出现不同的错误:
class_a.ts(1,25): error TS2307: Cannot find external module '../shared'.
It seems the compiler is finding shared.ts the first time it is imported, but not the second time.
似乎编译器在第一次导入时找到了 shared.ts,但不是第二次。
I'm using tsc 1.4.1.0.
我正在使用 tsc 1.4.1.0。
Anything obvious I'm missing, here?
有什么明显的我遗漏了,在这里?
采纳答案by basarat
I can verify that this is indeeda bug:
我可以验证这确实是一个错误:
C:\Users\basaratsyed\Downloads\ts\fancyimport\first\second>node "C:\Program Files (x86)\Microsoft SDKs\TypeScript.4\tsc.js" --module commonjs class_b.ts
../../third/class_c.ts(1,25): error TS2307: Cannot find external module '../first/shared'.
That said it does not show up in the language service APIe.g. atom-typescriptwhich is probably why it hasn't been reported yet:
也就是说它没有出现在语言服务 API 中,例如atom-typescript这可能是它尚未被报告的原因:
Note : I've reported it here : https://github.com/Microsoft/TypeScript/issues/2193
注意:我在这里报告过:https: //github.com/Microsoft/TypeScript/issues/2193