You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Luxon DateTime类型报错:无法将命名空间‘DateTime’用作类型

解决TypeScript无法识别luxon DateTime类型的问题
  • 错误原因:导入方式错误导致TypeScript将DateTime识别为命名空间而非类型,或是luxon与@types/luxon版本不兼容。

  • 解决步骤:

    1. 使用正确的ES模块导入方式
      替换现有导入代码为:

      import { DateTime } from 'luxon';
      

      该方式导入后,DateTime可同时作为类实例和类型使用,你提供的示例代码无需额外修改。

    2. 若使用CommonJS风格导入
      需明确指定类型:

      const { DateTime } = require('luxon');
      type DateTime = typeof import('luxon').DateTime;
      
    3. 同步luxon与@types/luxon版本
      版本不兼容会引发类型定义异常,执行以下命令更新到匹配的最新版本:

      npm install luxon@latest @types/luxon@latest
      

内容的提问来源于stack exchange,提问作者John Souris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 15:43:23