我最近开始了一个新的打字稿项目,现在我想利用时刻范围。我已经安装了时刻范围和@ typings / moment-range,我已将这些行添加到我的文档顶部:
import * as moment from 'moment';
import { DateRange } from 'moment-range';
但是,我仍然遇到此错误:Property 'range' does not exist on type 'typeof moment'
这是我的tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"noEmitHelpers": false,
"noEmitOnError": true,
"lib": [
"es6",
"dom",
"es2015.iterable"
],
"baseUrl": ".",
"paths": {
"*": [
"./node_modules/tns-core-modules/*",
"./node_modules/*"
]
}
},
"exclude": [
"node_modules",
"platforms",
"**/*.aot.ts"
]
}
我错过了哪一步?
另外,有没有办法全局包含时刻范围类型,以便我不必在每个文件中都这样做?
答案 0 :(得分:1)
好的,我明白了。我创建了一个这样的文件:
import * as moment from "moment";
import { DateRange } from 'moment-range';
declare module "moment" {
function range(range: string | Date[] | moment.Moment[]): DateRange;
function range(start: Date | moment.Moment, end: Date | moment.Moment): DateRange;
}
我在主文件中引用了一次,现在我可以在任何地方使用moment.range()
答案 1 :(得分:0)
看起来你正试图在瞬间调用范围。而不是时刻范围。
如果您在npmjs上查看他们的文档,则说要导入这样的时刻范围:
import Moment from 'moment';
import { extendMoment } from 'moment-range';
const moment = extendMoment(Moment);
你是在延长时刻吗? 你还在做什么样的项目?