简体   繁体   中英

Operator '>=' cannot be applied to types 'Duration' and 'number'.(2365)

I am getting this operator >= error in my vscode where I am unable to understand whats going on. I am trying to convert this code sample from moment into luxon but suddenly i am getting this error and my expected result is not coming true as I am getting in moment. My example code sample are given below, please take a look:

This is moment example:

const lastActivityLoaded1 = 1;

// moment().diff(lastActivityLoaded, 'hours') >= 48 && !canGetAllActivities()

console.log(`This keys moment Method : `, moment().diff('hours') >= 48); // false
console.log(
  `This keys moment Method 2 : `,
  moment().diff(lastActivityLoaded1, 'hours') >= 48
); // true

This is luxon example:

const lastActivityLoaded = 1;

console.log(
  `This keys luxon Method 2 :  `,
  luxon.DateTime.fromISO().diff({ hours: lastActivityLoaded }) >= 48
); // false 

Here I am expecting true as output but I am getting false.

Here, when I hover my mouse pointer I am getting this error,

Operator '>=' cannot be applied to types 'Duration' and 'number'.(2365) Expected 1-2 arguments, but got 0.(2554) datetime.d.ts(635, 20): An argument for 'text' was not provided. (method) DateTime.fromISO(text: string, opts?: luxon.DateTimeOptions): luxon.DateTime

Here I am expecting true as output but I am getting false.

See https://moment.github.io/luxon/#/moment for a guide on how to migrate from moment.js to Luxon.

Your first issue is with DateTime.fromISO() . The fromISO method expects a string as an argument. To replicate the functionality of calling moment without any arguments, use DateTime.now() instead.

Secondly, Luxon's diff method returns a Duration instance instead of a number like moment's does. To get the same functionality, get the milliseconds from the Duration . Also, the first argument to diff must be a DateTime instance, so you need to create one with the hours added and pass that.

luxon.DateTime.now().diff(
  luxon.DateTime.now().plus({ hours: lastActivityLoaded }), 
  'hours'
).milliseconds >= 48

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM