我最近开始了一个新的打字稿项目,现在我想利用时间范围。我安装了矩-范围和@类型/矩-范围,并将这些行添加到文档的顶部:
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"
]
}我错过了哪一步?
另外,是否有一种方法可以在全局范围内包含时间范围类型,这样我就不必在每个文件中都这样做了?
发布于 2017-08-18 13:43:24
好吧,我想出来了。我创建了这样一个文件:
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()
发布于 2017-08-18 12:05:11
看上去你是在试着在瞬间调用范围。而不是瞬间范围。
如果您查看他们在npmjs上的文档,就会看到这样的导入时间范围:
import Moment from 'moment';
import { extendMoment } from 'moment-range';
const moment = extendMoment(Moment);你在延长时间吗?还有,你在从事什么样的项目?
https://stackoverflow.com/questions/45755393
复制相似问题