简体   繁体   English

环境 Typescript 定义未捆绑在库中

[英]Ambient Typescript Definitions Not Bundled in Library

I'm working with the TSDX tool to build a react-typescript library.我正在使用 TSDX 工具来构建一个 react-typescript 库。 I have a number of types exported in my @types/ directory which are clearly picked up by the compiler as I use them within the app with no trouble.我在我的@types/目录中导出了许多类型,当我在应用程序中毫无问题地使用它们时,编译器清楚地选择了这些类型。

However, during my build, they do not get copied into my output bundle, which surprises me, since they are referenced through the code (which does get bundled correctly).然而,在我的构建过程中,它们并没有被复制到我的 output 包中,这让我感到惊讶,因为它们是通过代码引用的(确实被正确地捆绑了)。

// tsconfig.json
{
  "include": ["src", "types", "test"],
  "compilerOptions": {
    "target": "es5",
    "module": "esnext",
    "lib": ["dom", "esnext"],
    "importHelpers": true,
    "declaration": true,
    "sourceMap": true,
    "rootDir": "./",
    "strict": true,
    "noImplicitAny": false,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": false,
    "noFallthroughCasesInSwitch": true,
    "moduleResolution": "node",
    "baseUrl": "./",
    "paths": {
      "*": ["src/*", "node_modules/*"]
    },
    "jsx": "react",
    "esModuleInterop": true
  }
}

This is actually, believe it or not, by design.这实际上是设计使然,信不信由你。

You can define the location of your types in tsconfig.json and it is expected that they are always in the final build (by the fact that they are not moved).您可以在 tsconfig.json 中定义类型的位置,并且预计它们总是在最终构建中(因为它们没有被移动)。 ie given this structure:即给定这个结构:

src/app.ts
types/app.d.ts

Typescript would expect your published build to be something like: Typescript 期望您发布的构建类似于:

build/app.js
build/app.d.ts
types/app.d.ts

To achieve this you would either define a path to your ambient types within tsconfig为此,您可以在 tsconfig 中定义环境类型的路径

{
    baseUrl: "./",
    paths: [
        "my-app": "./types/app.d.ts"
    ]
}

or add a typeRoot that would include all types within a directory, careful with this, it will override any existing typeRoots such as those to node_modules/@types so you need to redeclare them.或添加一个包含目录中所有类型的 typeRoot,小心这一点,它将覆盖任何现有的 typeRoots,例如node_modules/@types的那些,因此您需要重新声明它们。

{
    typeRoots: ['./types', 'node_modules/@types']
}

and then consume the types using an import of my-app if you've declared a path.如果您已声明路径,则使用my-app的导入来使用类型。

It would be expected that your final build would include the types directory in the root where it was defined in your tsconfig.json .预计您的最终构建将包含根目录中的types目录,该目录在您的tsconfig.json中定义。

I suspect that you are including your .d.ts file within your applications source directory, and as such not expecting it to ever make it to a production build.我怀疑您将.d.ts文件包含在您的应用程序源目录中,因此不希望它能够用于生产构建。 In this case you would need to manually move it to the location that it is needed in your build or automate it with some other plugin depending on your build tooling.在这种情况下,您需要手动将其移动到构建中所需的位置,或者根据您的构建工具使用其他插件自动化它。

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

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