typescript 使用类型安全定义打字稿泛型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17655607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:50:19  来源:igfitidea点击:

Defining typescript generics with type safety

genericstypescript

提问by Tim

Can you define generics with safe types, as you can with c#?

你能用安全类型定义泛型吗,就像用 c# 一样?

E.g.

例如

public bool Foo<T>() where T : struct { /* */ }

Typescript now has generics, but can I perform a similar action?

Typescript 现在有泛型,但我可以执行类似的操作吗?

Thanks.

谢谢。

回答by Tim

Ok it seems you can do this:

好吧,看来你可以这样做:

Foo<T extends IBar>() { /* */ }

And that seems to make all calls require the T to implement IBar.

这似乎使所有调用都需要 T 来实现IBar