【问题标题】:Could not find a declaration file for module with module style declaration file找不到具有模块样式声明文件的模块的声明文件
【发布时间】:2021-01-18 10:01:51
【问题描述】:

我有一个根目录 @typessrc 文件夹

// @types/index.d.ts
import type { Profile } from "passport-discord"

declare module "fastify" {
  // eslint-disable-next-line @typescript-eslint/no-empty-interface
  interface PassportUser extends Profile {}
}

declare module "@elastic/ecs-pino-format"

// @TODO: remove when https://github.com/pinojs/pino-elasticsearch/issues/54 has been fixed
declare module "pino-elasticsearch"

在我的tsconfig.json 中,我尝试添加"include": ["@types", "src"],,但运行我的构建命令

"build": "rimraf dist && NODE_ENV=development npm run nexus:reflect && tsc",
"nexus:reflect": "SHOULD_EXIT_AFTER_GENERATE_ARTIFACTS=true ts-node --transpile-only src/schema.ts",

结果

daniell@DESKTOP-EKQSHNG:~/GitHub/akira$ npm run build

> akira@1.0.0 build
> rimraf dist && NODE_ENV=development npm run nexus:reflect && tsc


> akira@1.0.0 nexus:reflect
> SHOULD_EXIT_AFTER_GENERATE_ARTIFACTS=true ts-node --transpile-only src/schema.ts

Generated Artifacts:
          TypeScript Types  ==> /home/daniell/GitHub/akira/node_modules/@types/nexus-typegen/index.d.ts
          GraphQL Schema    ==> (not enabled)
src/utilities/logger.ts:1:23 - error TS7016: Could not find a declaration file for module '@elastic/ecs-pino-format'. '/home/daniell/GitHub/akira/node_modules/@elastic/ecs-pino-format/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/elastic__ecs-pino-format` if it exists or add a new declaration (.d.ts) file containing `declare module '@elastic/ecs-pino-format';`

1 import ecsFormat from "@elastic/ecs-pino-format"
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~

src/utilities/logger.ts:3:25 - error TS7016: Could not find a declaration file for module 'pino-elasticsearch'. '/home/daniell/GitHub/akira/node_modules/pino-elasticsearch/lib.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/pino-elasticsearch` if it exists or add a new declaration (.d.ts) file containing `declare module 'pino-elasticsearch';`

3 import pinoElastic from "pino-elasticsearch"
                          ~~~~~~~~~~~~~~~~~~~~


Found 2 errors.

【问题讨论】:

    标签: typescript tsconfig tsc


    【解决方案1】:

    使用带有files 选项的ts-node 怎么样?

     - "nexus:reflect": "SHOULD_EXIT_AFTER_GENERATE_ARTIFACTS=true ts-node --transpile-only src/schema.ts",
     + "nexus:reflect": "SHOULD_EXIT_AFTER_GENERATE_ARTIFACTS=true ts-node --transpile-only --files src/schema.ts",
    

    文档说ts-node 在默认情况下不会从tsconfig.json 加载filesincludeexcludehttps://github.com/TypeStrong/ts-node#help-my-types-are-missing

    【讨论】:

    • 谢谢,这是一个很好的答案,或者我通过将每个“声明模块”放在一个单独的文件中来修复它,以便它们不包含导入/导出,因此在任何地方都可见
    猜你喜欢
    • 2017-08-13
    • 2021-01-02
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-26
    相关资源
    最近更新 更多