Moment (). add () only works with literal values

I use Moment.js in TypeScript (with Angular 2, if that matters). When I use the add () method with literal values ​​as arguments, it works fine:

moment().add(1, 'month');

However, if I try to replace units with a string, this will not work:

let units:string = 'month';
moment().add(1, units);

with this error:

Argument of type '1' is not assignable to parameter of type 'DurationConstructor'.

What am I doing wrong here?

+14
source share
7 answers

Obsolete reverse add(unit: unitOfTime.DurationConstructor, amount: number|string)overload add(unit: unitOfTime.DurationConstructor, amount: number|string)creates ambiguity.

You can fix this by defining the type unitsas DurationConstructorinstead of string:

let units: moment.unitOfTime.DurationConstructor = 'month';
moment().add(1, units);

- const let, :

const units = 'month';
moment().add(1, units);
+20

, mins. def DurationConstructor -

namespace unitOfTime {
type Base = (
  "year" | "years" | "y" |
  "month" | "months" | "M" |
  "week" | "weeks" | "w" |
  "day" | "days" | "d" |
  "hour" | "hours" | "h" |
  "minute" | "minutes" | "m" |
  "second" | "seconds" | "s" |
  "millisecond" | "milliseconds" | "ms"
);

, :

refTime.clone().subtract(15, 'minute')

-k

+2

- . , , . , unitOfTime , .

import { unitOfTime } from 'moment';
import * as moment from 'moment';

option = {val: 30, unit: 'm'}
moment().add( this.querySince.val, <unitOfTime.DurationConstructor>this.querySince.unit )
+2

add

  1. add(amount?: DurationInputArg1, unit?: DurationInputArg2): Moment;
  2. add(unit: unitOfTime.DurationConstructor, amount: number|string)

, . units:string, number|string , , , 'qwe' 'rty' string, DurationArg2 .

moment.add(1,'months) , 1 , .

, , , .

1.

import * as moment from 'moment'

calculateRangeFromEnd(end: Date, unitsAmount: moment.DurationInputArg1, unitsMeasureMoment: moment.DurationInputArg2): IDateRange {
    return {
      endDate: end,
      startDate: moment(end).subtract(unitsAmount, unitsMeasureMoment).toDate()
    }
  }

2.

import * as moment from 'moment'

calculateRangeFromEnd(end: Date, unitsAmount: number | string, unitsMeasureMoment: moment.unitOfTimes.DurationConstructor): IDateRange {
    return {
      endDate: end,
      startDate: moment(end).subtract(unitsMeasureMoment, unitsAmount).toDate()
    }
  }
+2

add, subtract etc . :

moment.d.ts

add(amount?: DurationInputArg1, unit?: DurationInputArg2): Moment;
/**
 * @deprecated reverse syntax
 */
add(unit: unitOfTime.DurationConstructor, amount: number|string): Moment;

, , general ( "", "" ..).

function convertToDuration (unit: string): Moment.unitOfTime.DurationConstructor {

if(unit ==  'seconds' || unit ==  'minutes' || unit ==  'hours' || unit ==  'days' || unit ==  'weeks' || unit ==  'months'){
    return unit;
}
else // Default unit is hours
    return 'hours';
}

:

const time = moment().add(expiryTime.value, convertToDuration(expiryTime.unit));

expiryTime TypeScript:

interface ExpiryTimeDef {
value: number,
unit: string
}
0

let const

const units = 'month';
moment().add(1, units);

..

0

, deprecated reverse syntax of add, subtract, etc functions in moment deprecated reverse syntax of add, subtract, etc functions in moment. .

I have unitOfTime.Basea DurationInputArg2 field to unitOfTime.Basesince all of these methods have this final superclass, and that means that add, subtract, startOf, endOfI just need to remember as Base:

 import Base = moment.unitOfTime.Base

 theMoment.subtract(amount, unitString as Base)
 theMoment.startOf(unitString as Base)

The confusing part for most people is that it is amountflagged as an error. Fixed when one qualifies units

0
source

Source: https://habr.com/ru/post/1667382/


All Articles