我尝试通过指定文件夹名称来请求包含 index.js 的文件夹
import xyz = require("./xyz");
并收到编译错误“无法解析外部模块 xyz。模块无法别名为非模块类型。”
如果我指定索引
import xyz = require("./xyz/index");
它有效。
如果我这样做
var xyz = require("./xyz");
它有效,但我在 xyz 上没有智能感知。
Typescript 不支持该功能吗?
最佳答案
不,TypeScript 不遵循 Node.js 的模块解析规则,因为它们非常特定于该平台。 TypeScript 支持不遵循 Node.js 约定的多个平台。
TypeScript 规范规定,关于相对模块解析:
If the import declaration specifies a relative external module name, the name is resolved relative to the directory of the referencing module’s file path. The program must contain a module with the resulting file path or otherwise an error occurs. For example, in a module with the file path ‘C:/src/ui/main’, the external module names ‘./editor’ and ‘../lib/io’ reference modules with the file paths ‘C:/src/ui/editor’ and ‘C:/src/lib/io’.
关于node.js - 需要一个包含index.js的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21323956/