简体   繁体   中英

Typescript: is it possible to generate a ts file from declaration file and .js file?

I know declaration files are meant for libraries and not for projects.

But lets assume i have an existing js project that I want to migrate to .ts and I want to start by creating d.ts files for each file in my source.

That way,

1- I don't enforce my team to use typescript while getting the benefits of typescript compiler features in my IDE. People can keep committing .js files. And use of typescript in the project is left to the developer. By creating d.ts files for each file they can get the benefits of type safety. But it also hinders development in a sense that when a developer changes a js file, the d.ts file for that file also has to be changed. So as a shortcut i want to be able to generate a .ts file from existing d.ts and .js files and do the development there. It makes sense to create a d.ts file and .js file for committing to the remote source so that the source is left clean and approachable by people who are not used to typescript.

2- And can we generate d.ts files with dts-gen to have an head start, then add remaining types manually.

3- Also: compability with react.

4- And, what is the right way to generate dts files for a project that does not consist of a single file? What is the right way to do it in your experience?

So how possible is this pipeline? What are the chances of this working, and what are the barriers that are blocking us? What suggestions can I get for this?

Thanks in advance!

If you have a .d.ts file, you don't need to generate it - so instead of trying to get the compiler to generate the file, you can use a task runner to just copy the original .d.ts file to your desired location.

For example, have a Gulp task to copy the .d.ts from the source folder to your dist folder.

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