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

TypeScript导入自定义configuration模块时报ts2307找不到类型声明求助

问题解决方案
  • 首先确认自定义声明文件存放路径
    你的tsconfig.json中typeRoots配置了./types为自定义类型根目录,请将index.d.ts文件放置在项目根目录下的types文件夹中,不要直接放在根目录或其他非指定目录下。
  • 补充tsconfig.json缺失配置
    你当前的配置缺少baseUrl和文件匹配规则,修改配置如下:
    {
      "compilerOptions": {
        "allowJs": true,
        "checkJs": true,
        "noEmit": true,
        "target": "es2015",
        "module": "commonjs",
        "resolveJsonModule": true,
        "strict": false,
        "baseUrl": ".", // 新增:指定模块解析的根路径
        "typeRoots": ["./types", "./node_modules/@types"]
      },
      "include": ["./**/*", "types/**/*"] // 新增:指定TS需要扫描的文件范围,可按需调整源码目录匹配规则
    }
    
  • 修复声明文件内部依赖问题
    你声明的ICommand类型用到了Message类型,如果该类型来自discord.js,需要在index.d.ts顶部导入该类型,否则会导致整个声明文件解析失败,模块无法被识别:
    import type { Message } from 'discord.js' // 新增导入
    declare module 'configuration' {
      // 保留你原来的类型声明内容
    }
    
  • 备用方案:改用相对路径导入类型
    如果不想使用虚拟模块写法,可去掉index.d.ts外层的declare module 'configuration'包裹,直接导出类型,导入时使用相对路径即可:
    // types/index.d.ts 去掉模块包裹后的写法
    import type { Message } from 'discord.js'
    export type IConfiguration = {
      // 原有属性内容
    }
    export type IContext = '[Bot]' | '[Client]' | '[Server]'
    export type ICommand = (message: Message, arguments: string[]) => void | any 
    
    导入时写法调整为:
    import type { IConfiguration, ICommand } from './types/index'
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 22:24:02