我想知道在打字稿中是否有可能将Date()
转换为moment().tz("America/Los_Angeles").format();
我尝试过
import { MomentTimezone } from 'moment-timezone';
const moment : MomentTimezone = new MomentTimezone(Date(), "America/Los_Angeles");
但是我有这个错误:
S2693: 'MomentTimezone' only refers to a type, but is being used as a value here
做的时候
import * as moment from "moment-timezone";
let now = moment();
我有此错误:
TS2349: This expression is not callable. Type 'typeof import("C:/Users/sandro/IdeaProjects/booking/node_modules/moment/moment.d.ts")' has no call signatures.
和
const moment: MomentTimezone = {date: Date(), timezone: 'America/Los_Angeles'};
但我知道了
TS2322: Type '{ date: string; timezone: string; }' is not assignable to type 'MomentTimezone'. Object literal may only specify known properties, and 'date' does not exist in type 'MomentTimezone'
此代码的问题—
import * as moment from "moment-timezone";
-是在ES6中,import * as moment
语法将始终将moment
导入为non-callable模块。
TypeScript过去是不兼容的,无论如何,您都可以调用这些导入。但这不会在今天的TS中实现。相反,我们使用较短的代码:
import moment from "moment-timezone";
如果使用该代码,则moment().tz("America/Los_Angeles").format()
应该有效。
另请参见: