简体   繁体   English

发布TypeScript React组件时有关TS类型的问题

[英]Question about TS typings when publishing a TypeScript React Component

I'm working on making my first open source React Component that is sourced in TypeScript. 我正在制作我的第一个开源Type组件中的开源React组件。 I'll be transpiling to .js for distribution (aka package.json "main": "build/index.js"). 我将转换为.js进行分发(又名package.json“ main”:“ build / index.js”)。

Since the source code is already in TypeScript, the typings for the component are already defined in my source, but I'm assuming anyone consuming this project will not get these for free. 由于源代码已经在TypeScript中,因此在我的源代码中已经定义了组件的类型,但是我假设使用此项目的任何人都不会免费获得这些类型。 Do I have to create an index.d.ts in my package root and re-export my typings, or is there another way for code that is originally written in TypeScript to begin with? 我是否必须在程序包的根目录中创建index.d.ts并重新导出我的键入内容,还是用最初以TypeScript编写的代码还有另一种方法?

You'll need a .d.ts file, but you don't need to do it manually. 您需要一个.d.ts文件,但不需要手动进行。 It can be generated automatically as part of your build process. 它可以在生成过程中自动生成。 In your tsconfig.json, add the following line: 在您的tsconfig.json中,添加以下行:

"declaration": true,

And so that those consuming your code know where to find the declaration file, add the following in your package.json. 为了使使用您代码的人知道在哪里可以找到声明文件,请在package.json中添加以下内容。

"types": "build/index.d.ts",

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

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