【问题标题】:yarn build --production 会导致错误,而 yarn build 不会
【发布时间】:2022-01-21 10:19:15
【问题描述】:

我试图找出原因,当我尝试 yarn install --production 然后 yarn build 我的 ts 反应应用程序时,它会导致 Could not find a declaration file for module 'react-router-dom'. '..../node_modules/react-router-dom/index.js' implicitly has an 'any' type. 虽然它已经包含在 deps 中:

yarn install 然后yarn build 成功。

【问题讨论】:

  • 这些是开发依赖项。请也显示依赖项
  • 我已经更新了依赖项。我安装了 yarn add @types/react-router-dom,它归于开发部门。

标签: reactjs typescript yarnpkg


【解决方案1】:

在根 index.d.ts 文件中包含 types 包。您可以使用任何未与其 sans @type/ 等效依赖项配对的 @types 包来执行此操作

您可以将这种方法用于分析和 sdks 之类的事情

它将defs编译成输出,使其在生产中可用

您还必须在 tsconfig.json 中设置 "declaration": true`

{
"compilerOptions": {
    "sourceMap": "true",
    "declaration": true,
    "...": "..."
  },
  "include": ["index.d.ts", "src/**/*", "..."]
}

这是我当前项目的根 index.d.ts

/// <reference types="facebook-js-sdk" />
/// <reference types="facebook-pixel" />
/// <reference types="google.maps" />
/// <reference types="google-libphonenumber" />
/// <reference types="gtag.js" />
/// <reference types="glob" />

declare module "facebook-js-sdk";
declare module "facebook-pixel";
declare module "google.maps";
declare module "google-libphonenumber";
declare module "gtag.js";
declare module "glob";

interface Window {
  dataLayer?: object[];
}

在额外的 tsconfig 文件 (tsconfig.build.json) 中,扩展您的主 tscnfig.json,然后调用 exclude 的路径

  • tsconfig.build.json
{
    "extends": "./tsconfig.json",
    "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}

注意:

一旦您声明了包,您就不再需要导入该包提供的类型。它们被注入并在全球范围内供消费

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-29
    • 2023-02-11
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 2018-09-03
    • 2020-10-08
    相关资源
    最近更新 更多