moment().add() only works with literal values
Another option to the accepted answer is typecasting in the argument. There's really no difference, just figured I'd include this answer as an option. Also unitOfTime can be imported from moment as a module if you want some more brevity.
import { unitOfTime } from 'moment';
import * as moment from 'moment';
option = {val: 30, unit: 'm'}
moment().add( this.querySince.val, <unitOfTime.DurationConstructor>this.querySince.unit )
Deprecated reverse overload add(unit: unitOfTime.DurationConstructor, amount: number|string)
creates ambiguity.
You can fix this by defining type of units
to be DurationConstructor
instead of string
:
let units: moment.unitOfTime.DurationConstructor = 'month';
moment().add(1, units);
Another option is just to use const
instead of let
, so literal type will be inferred:
const units = 'month';
moment().add(1, units);