Машинопись дата выпуска (VueJS) - PullRequest
0 голосов
/ 24 апреля 2020

Возникла очень странная проблема с 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 Ответ

1 голос
/ 24 апреля 2020

Для чего он стоит и что помогло, я нашел сравнение нового проекта со старым.

В старом проекте, если я буду следовать определению Даты, где он был определен, я был направлен к файлу: 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; выглядит как быть правильной реализацией.

Итак, опять же, понятия не имею, что случилось с этим. Я не играю в файлах ядра, поэтому не уверен, как этот файл «испорчен?». Еще раз спасибо за всех, кто так быстро это понял.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...