Skip to content

(typescript) Date property in reactive object can't assign to pure Date type #2 #4307

@dilepremon

Description

@dilepremon

Version

3.2.1

Reproduction link

https://github.com/BurnashevDmitry/date_error_vue_complier

Steps to reproduce

Example

src/CargoService.ts

interface ICargo {
  name: string;
  createdAt: Date;
  price: number;
}

export class Cargo implements ICargo {
  name: string;
  price: number;
  createdAt: Date;

  constructor(name = '', price = 0) {
    this.name = name;
    this.price = price;
    this.createdAt = new Date();
  }
}

export default class CargoService {
  static info(cargoData: Cargo): void {
    console.log(cargoData);
  }
}

App.vue

<script lang="ts">
import { defineComponent, ref } from 'vue';
import CargoService, { Cargo } from './CargoService';
export default defineComponent({
  data() {
    return {
      cargo: new Cargo('first', 100),
    };
  },
  methods: {
    info() {
      CargoService.info(this.cargo);
    },
  },
});
</script>

Error

src/App.vue:20:25
TS2345: Argument of type '{ name: string; price: number; createdAt: { toString: () => string; toDateString: () => string; toTimeString: () => string; toLocaleString: { (): string; (locales?: string | string[] | undefined, options?: DateTimeFormatOptions | undefined): string; }; ... 39 more ...; getVarDate: () => VarDate; }; }' is not assignable to parameter of type 'Cargo'.
  Types of property 'createdAt' are incompatible.
    Property '[Symbol.toPrimitive]' is missing in type '{ toString: () => string; toDateString: () => string; toTimeString: () => string; toLocaleString: { (): string; (locales?: string | string[] | undefined, options?: DateTimeFormatOptions | undefined): string; }; ... 39 more ...; getVarDate: () => VarDate; }' but required in type 'Date'.
    18 |   methods: {
    19 |     info() {
  > 20 |       CargoService.info(this.cargo);
       |                         ^^^^^^^^^^
    21 |     },
    22 |   },
    23 | });

What is expected?

All works fine

What is actually happening?

Property '[Symbol.toPrimitive]' is missing in type Data


Similar to vuejs/composition-api#279

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions