简体   繁体   English

在 @types 命名空间下使用自定义 typescript 类型为 NPM package *NOT*

[英]Consuming custom typescript typings as NPM package *NOT* under @types namespace

At my company we are wanting to automate the generation of our typings against out GraphQL API and package those up into a package we can reach for with all of our clients that use the API instead of relying on copy pasta. At my company we are wanting to automate the generation of our typings against out GraphQL API and package those up into a package we can reach for with all of our clients that use the API instead of relying on copy pasta. I've been reading through the TypeScript docs and am a little confuse on how to go about telling TypeScript about these custom typings.我一直在阅读 TypeScript 文档,对于如何告诉 TypeScript 这些自定义类型,我有点困惑。

From my readings TypeScript will automatically look for node_modules/@types/* .从我的读数 TypeScript 将自动寻找node_modules/@types/* Unfortunately since this is internal API and we don't want to publish under the DefinitelyTyped repository (I haven't found that you can't explicitly write and @types/package and it be a private module).不幸的是,因为这是内部 API 并且我们不想发布到definitelyTyped 存储库下(我还没有发现你不能显式地编写和@types/package并且它是一个私有模块)。 So at any rate this means whatever we were to name our package will not automatically be looked at by TypeScript.所以无论如何,这意味着无论我们如何命名 package,TypeScript 都不会自动查看。

I read about types and typeRoots thinking these might be the options I need to use.我阅读了有关typestypeRoots的内容,认为这些可能是我需要使用的选项。 From my reading types is a way that I can specify the @types I want TypeScript to include, a sort of cherry picking option.从我的阅读types中,我可以指定我希望 TypeScript 包含的@types ,这是一种樱桃采摘选项。 typeRoots seems like what I should use; typeRoots似乎是我应该使用的; however it says only packages under this option will be included.但是它说包含此选项下的软件包。 My assumption here is I would need to do something like the following (we would still want the definitions under @types ):我的假设是我需要执行以下操作(我们仍然需要@types下的定义):

{
  "compilerOptions": {
    "typeRoots": ["node_modules/@types", "node_modules/@companyOrg/customTypings"]
  }
}

Is anyone out there doing a similar thing and found a way for TypeScript to include your custom typings that are not under the @types umbrella?有没有人在做类似的事情并找到了一种方法让 TypeScript 包含不在@types保护伞下的自定义类型? Please note I'm not talking about creating a custom.d.ts in the project directory, but consuming an NPM packages similar to @types/react except something like @company/our-types .请注意,我不是在谈论在项目目录中创建custom.d.ts ,而是使用类似于@types/react的 NPM 包,除了类似@company/our-types

Current tsconfig.json :当前tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "preserve",
    "preserveSymlinks": true,
    "typeRoots": [
      "node_modules/@types",
      "node_modules/@companyOrg/customTypings",
    ],
  },
  "include": [
    "src"
  ],
}

Also I am working locally on this so I have used yarn link @company/our-types perhaps this is where the problem is originating from.我也在本地工作,所以我使用yarn link @company/our-types也许这就是问题的根源。 I have checked the path several times and everything seems fine though.我已经检查了几次路径,但一切似乎都很好。

Let me show the example that works for me.让我展示一个对我有用的例子。 There are 2 packages: types (it can be any name) and consumer.有 2 个包:类型(可以是任何名称)和消费者。 Types package contains my typings in form of d.ts files, and I have index.d.ts in the root (this file is used as an entry point by convention but it can be redefined using types field in package.json)类型 package 包含我的d.ts文件形式的类型,并且我在根目录中有index.d.ts (此文件按惯例用作入口点,但可以使用 package.json 中的types字段重新定义)

// types/index.d.ts
declare namespace MyLib {
    interface MyInterface {
        id: string;
    }
}

export = MyLib;

I do npm link types into consumer and add "types": "*" to dependencies of consumer.我将 npm 链接typesconsumer中,并将"types": "*"添加到消费者的dependencies项中。

{
  "name": "consumer",
  "version": "1.0.0",
  "dependencies": {
    "typescript": "^3.8.3",
    "types": "*" // <----
  }
}

Right after that I can use types exported from types in consumer :之后,我可以使用从consumer类型导出的types

// consumer/index.ts
import { MyInterface } from "types";

const y: MyInterface = { id: "1" };

It works because TS is looking for types definitions in all dependencies and allows you to use them without any additional configuration.它之所以有效,是因为 TS 在所有依赖项中寻找类型定义,并允许您在没有任何额外配置的情况下使用它们。

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

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