简体   繁体   中英

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. I'll be transpiling to .js for distribution (aka 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. 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?

You'll need a .d.ts file, but you don't need to do it manually. It can be generated automatically as part of your build process. In your tsconfig.json, add the following line:

"declaration": true,

And so that those consuming your code know where to find the declaration file, add the following in your package.json.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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