【问题标题】:Typescript Types declaration not found找不到打字稿类型声明
【发布时间】:2022-01-06 11:36:54
【问题描述】:

当我在类型声明文件中遇到问题时,我正在使用 TypeScript 编写一个小型 Discord 机器人。

index.d.ts:

declare module 'configuration' {
    /**
     * Configuration of your bot.
     */
    export type IConfiguration = {
        /**
         * Token of your bot.
         */
        token: string
        /**
         * Prefix of your bot.
         */
        prefix: string
        /**
         * Time to cool down the bot commands.
         */
        coolDown: number
    }
    /**
     * Context at which we are logging.
     */
    export type IContext = '[Bot]' | '[Client]' | '[Server]'

    /**
     * Type for the commands
     */
    export type ICommand = (message: Message, arguments: string[]) => void | any 
}

显然 index.ts 无法加载声明文件?

执行import { IConfiguration, ICommand } from 'configuration'; 只会返回错误: Cannot find module 'configuration' or its corresponding type declarations.ts(2307)

我一直在网上寻找,提到的一些解决方案在 package.json 和 tsconfig.json 中列出了您的类型,但似乎都没有帮助。

TSConfig.json:

{
    "compilerOptions": {
      "allowJs": true,
      "checkJs": true,
      "noEmit": true,
      "target": "es2015",
      "module": "commonjs",
      "resolveJsonModule": true,
      // TBD it is desired to enabled strict type checking at some point:
      "strict": false,
      "typeRoots": ["./types", "./node_modules/@types"]
    }
}

我不知道为什么这不起作用。非常感谢。

【问题讨论】:

    标签: typescript ecmascript-6 types discord.js type-declaration


    【解决方案1】:

    找出发生了什么。

    显然 ES Module import 和 typescript declare module 不是很好的朋友...不知道为什么它不想工作,但是当我将所有类型导出到根级别时:

    /**
     * Type for the commands
     */
    export type ICommand = (message: Message, arguments: string[]) => void | any 
    
    /**
     * Context at which we are logging.
     */
    export type IContext = '[Bot]' | '[Client]' | '[Server]'
    
    /**
    * Configuration of your bot.
    */
    export type  IConfiguration = {
    
        /**
        * Token of your bot.
        */
        token: string
    
        /**
        * Prefix of your bot.
        */
        prefix: string
    
        /**
        * Time to cool down the bot commands.
        */
        coolDown: number
    }
    

    它工作得很好。如果有人知道为什么会这样,请告诉我!

    【讨论】:

      猜你喜欢
      • 2019-02-16
      • 1970-01-01
      • 2017-08-12
      • 2021-01-20
      • 2017-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多