【问题标题】:TypeScript relative module names resolutionTypeScript 相对模块名称解析
【发布时间】:2016-03-28 02:32:00
【问题描述】:

我使用 TypeScript 1.8,文件结构如下:

src
  reducers
    index.ts
    someReducer.ts
  app.tsx

reducers/index.ts

import { combineReducers } from "redux";
import someReducer from "./someReducer";

const appReducer = combineReducers({
    someReducer
});

export default appReducer;

app.tsx 我尝试像这样导入它:

import appReducer from "./reducers";

但是,我收到一个错误:

无法归档模块“./reducers”

我查看了Modules Resolution 文章,上面说 TypeScript 会查看文件夹中的 index.ts,但显然不会?

【问题讨论】:

    标签: module typescript


    【解决方案1】:

    index.ts 只有在使用--moduleResolution node 时才会被发现和使用,并且只有在 Node.js 中或使用与 Node.js 实现相同语义的模块加载器时才会在运行时工作。 (换句话说,你最好明确指定文件,而不是依赖于 Node.js 特定的语义。)

    【讨论】:

    • 它与 ES6 兼容性如何对应,因为这是那里的标准语法?
    • 我添加了 "moduleResolution": "node", 和我的 tsconfig.json,这对我有帮助,谢谢。
    • ES6 没有定义如何解析模块 ID。它只定义了模块格式。目前还没有任何关于如何实际解析模块的标准(最接近通用互操作标准的是 AMD;有一个 WHATWG 委员会单独研究此问题)。
    猜你喜欢
    • 1970-01-01
    • 2017-04-10
    • 2018-11-10
    • 2015-08-22
    • 2017-09-21
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多