繁体   English   中英

Typescript 未找到类型声明

[英]Typescript Types declaration not found

当我在类型声明文件中遇到问题时,我正在 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"]
    }
}

我不知道为什么这不起作用。 提前谢谢了。

发现发生了什么。

显然 ES 模块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
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM