How to use typescript/flow in nodejs without compiling it

為{幸葍}努か 提交于 2021-02-16 13:29:10

问题


Can someone give me some advice or links for discussion on whether I should bundle JS for backend?

I tried to Google with this title (and similar words) and I can't get any useful links.

Just want to know, say I am using latest Node.JS (es6-ready), should I bundle/compile the JS? If not, how am I suppose to use typescript/flow?

Thank you.


回答1:


I feel like you are asking two different questions. I'll try to answer both.

How can I just run TypeScript code?

This is the one your question's title seems to ask ("How to use typescript/flow in nodejs without compiling it"). For this, you can use the ts-node package on npm. But it's usually not a good idea to use ts-node over just compiling when running in production because it tends not to be as fast.

How should TypeScript code get distributed to be run?

Any TypeScript code will need to get compiled from .ts files to .js files to eventually be run. Basically something like the same thing applies to Flow code.

If you plan to distribute a package written in TypeScript, you should be publishing the .js and .d.ts files together. This is so that

  1. Your package consumers don't have to recompile your package. (they already get .js files.
  2. Your non-TypeScript consumers don't need to install TypeScript to use your package. (they already have runnable .js files)
  3. Your TypeScript consumers can get good type safety and completions. (they get your .d.ts files)

For more information, see the TypeScript documentation on Publishing Declaration Files.



来源:https://stackoverflow.com/questions/40752666/how-to-use-typescript-flow-in-nodejs-without-compiling-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!