【发布时间】:2017-09-10 15:55:37
【问题描述】:
我使用这个 yeoman 生成器制作了一个带有 typescript 的 angular 1 项目:https://github.com/FountainJS/generator-fountain-systemjs
它使用 SystemJS 和 jspm 来获取依赖关系,但使用 npm 来定义类型(使用 distinctlyTyped 存储库)。
这几天我一直在努力尝试导入 moment 类型定义。
我已经使用 jspm 安装了 moment,我发现它带有自己的类型定义,所以如果你调用命令 npm install @types/moment --save-dev 你只会得到一个存根和一个弃用警告
这是 Moment 的存根类型定义 (https://github.com/moment/moment)。 Moment 提供了自己的类型 定义,所以你不需要安装@types/moment!
现在,我不知道这是我的编辑器、项目或打字稿设置还是真的打字稿问题,但我似乎无法正确导入时刻的类型定义。
如果我在 import moment from 'moment'; 或 import * as moment from 'moment'; 上执行此操作,我的编辑器上会出现此错误(atom with atom-typescript,但我在 Visual Studio Code 上也有同样的错误)Cannot find module 'moment'.
尽管有这个错误,当我构建我的应用程序时它工作正常(调用时刻函数有效)。
我已经尝试了很多我在互联网上找到的解决方案(显然,从现在开始导入类型定义是一个常见问题)但没有一个奏效。
昨天我设法通过在node_modules/@types 中手动创建目录moment 并将moment.d.ts 放入其中(不过我不得不将其重命名为index.d.ts)来使其工作。
因为我不是很喜欢这个解决方案,所以我想至少创建一个可以放入内容的类型文件夹,而无需修改 node_modules 结构。所以我创建了一个文件夹custom-types 并将带有类型定义的moment 文件夹放在那里,然后将custom-types 添加到tsconfig.json 中,我认为这会很好,但实际上错误再次出现......
现在我没有想法了,我真的不知道还能尝试什么。
这是我当前的tsconfig.json(在最后一次尝试中,我添加了具有不同路径的custom-types 文件夹,尽管它与node_modules 文件夹和tsconfig.json 文件处于同一级别)
{
"compilerOptions": {
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,
"module": "system",
"target": "es5",
"moduleResolution": "classic",
"typeRoots": [
"node_modules/@types/",
"./custom-types/",
"custom-types/",
"../custom-types/",
"../../custom-types/",
"../../../custom-types/"
],
"types": [
"angular-ui-router",
"angular",
"angular-mocks",
"jquery",
"jasmine",
"moment",
"es6-shim"
]
},
"compileOnSave": false,
"filesGlob": [
"custom-types/**/*.ts",
"src/**/*.ts",
"src/**/*.tsx",
"!jspm_packages/**",
"!node_modules/**"
]
}
【问题讨论】:
标签: typescript momentjs atom-editor