打字日期问题(VueJS)



创建新日期时Typescript出现了一个非常奇怪的问题。

<template>
<div> Testing Date</div>
</template>
<script lang="ts">
import Vue from "vue";
export default Vue.extend({
name: "Test",
methods: {
checkDate() {
console.log("...checkDate...");
const now = new Date();
console.log(now);
}
},
mounted() {
this.checkDate();
}
});
</script>
<style scoped></style>

我得到错误:

此表达式不可构造。类型"Date"没有构造签名

这不是创建Date对象的新实例的有效方法吗?

我在比较新项目和旧项目时发现了它的价值和帮助。

在旧项目中,如果我遵循Date的定义,我会被引导到文件:node_modules/typescript/lib/lib.es5.d.ts,定义为:

interface DateConstructor {
new(): Date;
new(value: number | string): Date;
new(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): Date;
(): string;
readonly prototype: Date;
/**
* Parses a string containing a date, and returns the number of milliseconds between that date and midnight, January 1, 1970.
* @param s A date string
*/
parse(s: string): number;
/**
* Returns the number of milliseconds between midnight, January 1, 1970 Universal Coordinated Time (UTC) (or GMT) and the specified date.
* @param year The full year designation is required for cross-century date accuracy. If year is between 0 and 99 is used, then year is assumed to be 1900 + year.
* @param month The month as a number between 0 and 11 (January to December).
* @param date The date as a number between 1 and 31.
* @param hours Must be supplied if minutes is supplied. A number from 0 to 23 (midnight to 11pm) that specifies the hour.
* @param minutes Must be supplied if seconds is supplied. A number from 0 to 59 that specifies the minutes.
* @param seconds Must be supplied if milliseconds is supplied. A number from 0 to 59 that specifies the seconds.
* @param ms A number from 0 to 999 that specifies the milliseconds.
*/
UTC(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): number;
now(): number;
}
declare var Date: Date;

在上面的代码块中,declare var Date: Date;似乎是罪魁祸首。

在新项目中,如果我遵循日期定义,我会被发送到:node_modules/typescript/lib/lib.es5.d.ts(完全相同的文件(,但日期的定义现在是:

interface DateConstructor {
new(): Date;
new(value: number | string): Date;
new(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): Date;
(): string;
readonly prototype: Date;
/**
* Parses a string containing a date, and returns the number of milliseconds between that date and midnight, January 1, 1970.
* @param s A date string
*/
parse(s: string): number;
/**
* Returns the number of milliseconds between midnight, January 1, 1970 Universal Coordinated Time (UTC) (or GMT) and the specified date.
* @param year The full year designation is required for cross-century date accuracy. If year is between 0 and 99 is used, then year is assumed to be 1900 + year.
* @param month The month as a number between 0 and 11 (January to December).
* @param date The date as a number between 1 and 31.
* @param hours Must be supplied if minutes is supplied. A number from 0 to 23 (midnight to 11pm) that specifies the hour.
* @param minutes Must be supplied if seconds is supplied. A number from 0 to 59 that specifies the minutes.
* @param seconds Must be supplied if milliseconds is supplied. A number from 0 to 59 that specifies the seconds.
* @param ms A number from 0 to 999 that specifies the milliseconds.
*/
UTC(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): number;
now(): number;
}
declare var Date: DateConstructor;

DateConstructor接口看起来是相同的,但declare var Date: DateConstructor;似乎是正确的实现。

所以,再一次,不知道发生了什么。我不在核心文件中玩,所以不确定这个文件是如何"损坏的?"。再次感谢所有这么快就参与进来的人。

最新更新