简体   繁体   中英

Writing libraries with typescript and generics

So I'm writing a library with typescript and obviously, most people who will use it will be using javascript. My question is if I want to have a generic function in my library for example: testFunction<TestType>("hello world") If transpiled that into javascript, how would people using javascript interact with that generic function? Will I have to not use generics for the sake of the javascript user's? or does typescript have a way of making it work for javascript users?

Once your TypeScript is transpiled to JavaScript, downstream clients will lose the type information. In other words, JavaScript clients will just see the equivalent of testFunction("helloWorld"); . For downstream clients that are using TypeScript, they will be able to gain benefits (static analysis, type checks, auto-completion) from using your types as defined in your package.json.

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