打字稿日期发布(VueJS)

时间:2020-04-24 16:05:10

标签: javascript typescript date vue.js

在创建新日期时,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对象的新实例的有效方法吗?

1 个答案:

答案 0 :(得分:1)

对于它的价值和帮助之处,我发现新项目与旧项目进行了比较。

在旧项目中,如果我遵循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;似乎是正确的实现。

因此,再一次,不知道发生了什么。我不在核心文件中闲逛,所以不确定该文件如何“损坏”。再次感谢所有如此迅速参与的人。