简体   繁体   English

用打字稿和泛型编写库

[英]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. 因此,我正在使用Typescript编写一个库,显然,大多数将使用它的人都将使用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? 我的问题是,例如,我是否想在我的库中有一个通用函数: testFunction<TestType>("hello world")如果将其转换为javascript,使用javascript的人将如何与该通用函数交互? Will I have to not use generics for the sake of the javascript user's? 我是否不必为了JavaScript用户而使用泛型? or does typescript have a way of making it work for javascript users? 还是打字稿有办法让它适用于javascript用户?

Once your TypeScript is transpiled to JavaScript, downstream clients will lose the type information. 将您的TypeScript转换为JavaScript后,下游客户端将丢失类型信息。 In other words, JavaScript clients will just see the equivalent of testFunction("helloWorld"); 换句话说,JavaScript客户端将只看到等效的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. 对于使用TypeScript的下游客户端,他们将能够通过使用package.json中定义的类型来获得好处(静态分析,类型检查,自动完成)。

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

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