首页 > 解决方案 > 带有 Typescript 的 Luxon:TS2339:“DateTime”类型上不存在属性“c”

问题描述

我一直在尝试将 JS 函数转换为 TS,我在其中使用Luxon

问题是,当我尝试从now()c访问该属性时,出现以下错误Typescript: TS2339: Property 'c' does not exist on type 'DateTime'.

import { DateTime as Luxon } from 'luxon';

const getCurrentHour = (): number => {
  const dTime = Luxon.now();
  return dTime.c.hour;
}

console.log(typeof Luxon.now()); // I get "DateTime"
console.log(Luxon.now()); // I can see the "c" property

我知道 JavaScript 的 DateTime 接口没有这个c属性,但是我如何设法用 Luxon 和 TypeScript 编写这个代码呢?我已经安装了@types/luxon并试图在那里找到一个类型导出,但没有成功。

标签: typescriptluxon

解决方案


我有同样的问题。尝试return dTime.hour代替return dTime.c.hour


推荐阅读