javascript - Typescript 日期问题 (VueJS)

标签 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 对象的新实例的有效方法吗?

最佳答案

我发现将新项目与旧项目进行比较后发现它的值(value)和那些对这里有帮助的内容。

在旧项目中,如果我遵循 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 接口(interface)似乎相同,但 declare var Date: DateConstructor; 似乎是正确的实现。

所以,再次,不知道发生了什么。我不玩核心文件,所以不确定这个文件是如何“损坏”的?再次感谢所有如此迅速参与进来的人。

关于javascript - Typescript 日期问题 (VueJS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61412594/

相关文章:

mysql - Powershell - 将日期格式修改为 CSV 列中的值

javascript - 我想以时间戳的形式将日期设置为无限

javascript - Angular 5-如何向构造函数添加多个参数?

html - 错误错误 : ExpressionChangedAfterItHasBeenCheckedError in Angular 5

python - 在Python 3中计算贷款的结束日期

java - java中如何将给定的日期转换为年,月,日

javascript - 是否可以键入装饰器添加的属性?

php - Jquery 可排序,未定义数量的列表

javascript - 可以使浏览器不删除字符

javascript - Cypress 遍历数组中的元素