val*_*epu 10 momentjs typescript atom-editor
我使用这个yeoman生成器创建了一个带有
typescript 的角度1项目:https://github.com/FountainJS/generator-fountain-systemjs
它使用SystemJS和jspm获取依赖关系,但使用npm进行类型定义(使用DefinitelyTyped存储库).
在最近几天我一直在努力尝试导入时刻类型定义.我已经使用jspm安装了一下,我发现它带有自己的类型定义,所以如果你调用命令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-typescript,但我在Visual Studio代码上有相同的错误)Cannot find module 'moment'.
尽管有这个错误,当我构建我的应用程序时它工作正常(调用时刻函数工作).
我已经尝试了很多我在互联网上找到的解决方案(显然,从瞬间导入类型定义是一个常见的问题),但没有一个工作.昨天我设法通过手动创建moment内部目录node_modules/@types并将其放入其中来使其工作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/**"
]
}
Run Code Online (Sandbox Code Playgroud)
我一直在使用momentTypeScript(IntelliJ),没有额外的工作来安装它的打字.
这是相关的配置:
tsconfig.json:
"moduleResolution": "Node""types"部分./package.json 没有 @types/moment./node_modules/moment/:
./moment.d.ts 存在./package.json 具有 "typings": "./moment.d.ts"./package.json 是 "version": "2.14.1"当我切换回时"moduleResolution": "Classic",TypeScript说cannot find module 'moment'.所以这可能是罪魁祸首.