Typescript无法找到已使用SystemJS映射的模块

时间:2016-05-06 20:21:43

标签: typescript systemjs tsc tsconfig

Typescript在此导入中找不到该模块import ga from 'googleAnalytics';

SystemJS知道在哪里找到模块,因为它已经像前面那样被映射出来:

map: {
    '@angular': 'node_modules/@angular',
    'rxjs': 'node_modules/rxjs',
    'underscore': 'node_modules/underscore/underscore-min.js',
    'googleAnalytics': '//www.google-analytics.com/analytics.js'
},

如何提供与tsc相似的映射?

这个问题似乎指向了一个好的方向:How to avoid imports with very long relative paths in Angular 2?

Typescript 2.0似乎支持tsconfig.jsonpaths配置。有没有办法下载Typescript 2.0?我可以为path配置提供http网址(//www.google-analytics.com/analytics.js),就像我使用SystemJS一样吗?如果没有办法下载Typescript 2.0,我怎样才能达到我想要的当前版本?

修改

我得到的具体错误是:"无法找到模块' ga'"。

这是我的tsconfig.json

{
    "compilerOptions": {
        "rootDir": "./",
        "target": "es5",
        "module": "system",
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "noImplicitAny": true
    },
    "exclude": [
        "node_modules",
        "front-end/node_modules",
        "typings/main",
        "typings/main.d.ts"
    ]
}

2 个答案:

答案 0 :(得分:1)

可以使用Typescript Type Definitions解决此问题。

使用定义:

  1. 将类型定义放在目录中。预制版由DefinitelyTyped提供。
  2. 将此行添加到需要导入Google Analytics的.ts文件中:

    /// <reference path="ga.d.ts" />
    
  3. 更新

    当我跑tsc时,它对我有用。对不起,我忘了添加:

    1. ga和SystemJS映射中重构为import
    2. 这并不会使编译器知道它需要检查SystemJS文件,而只是作为您导入的模块的占位符,因此不会抛出任何错误,我们可以在运行时使用SystemJS实际解析模块。

答案 1 :(得分:0)

我想你明白,在打字稿编译的情况下,转换器正在寻找确定类型的文件。他需要知道外部模块的类型定义。我的情况下你有活动的nodejs模块解析(你有)你使用非相对路径(你这么做)你必须添加到你的项目node_modules目录(也许你已经有一个)和这个目录添加googleAnalytics.d.ts文件或您可以创建node_modules / googleAnalytics目录,然后在那里添加index.d.ts。可以从DefintelyTyped repository

下载Google Analytics的定义类型

有关模块解析here

的更多信息

编辑:根据您的评论,您可能需要将模块导出添加到Google定义文件

declare module 'googleAnalytics' {
    export = UniversalAnalytics;
}