简体   繁体   English

有人可以解释一下吗<otherthing> Typescript 或 Angular 中的语法?

[英]Can someone explain Thing<otherthing> syntax in Typescript or Angular?

I see this kind of syntax in Angular/Typescript demos:我在 Angular/Typescript 演示中看到了这种语法:

Thing<otherthing>

Examples:例子:

Observable<any>
Observable<boolean>

What is going on here?这是怎么回事? What are the things on the left of the first < and what are the things inside?第一个<左边的东西是什么,里面的东西是什么? I'm not a complete moron here so I can see from these examples that there are variable types inside the <> and I get the concept in general of an Observable.我在这里不是一个完整的白痴,所以我可以从这些示例中看到 <> 中有变量类型,并且我大致了解了 Observable 的概念。 What i'm asking here are what are the other things that could be either on the inside or on the left?我在这里要问的是,可能在内部或左侧的其他东西是什么? ie, can i replace the word "Observable" with something else?即,我可以用别的东西代替“Observable”这个词吗? Is there a list of those something else's somewhere that I can look up?是否有我可以查找的其他地方的列表? Can i replace "any" and "boolean" with other variable types, Is there a list of those?我可以用其他变量类型替换“any”和“boolean”,是否有这些类型的列表? Also I've seem something like this:我也似乎是这样的:

Thing<otherthing<yetanotherthing>>

What is this referring to?这是指什么?

I don't even know if my question is Typescript related or Angular specific.我什至不知道我的问题是与 Typescript 相关还是特定于 Angular。 Sorry for the vagueness here it's just not well explained in any of the documentation I've been able to find.抱歉,这里的含糊不清,只是在我能找到的任何文档中都没有得到很好的解释。

That is a Generic type.那是一个泛型类型。 If you have Observable<T> it means you have an Observable of type T where T can be any type, custom or otherwise.如果您有Observable<T>则意味着您有一个T类型的Observable ,其中T可以是任何类型、自定义或其他类型。 So Observable<boolean> is an Observable of booleans (a stream of boolean values).所以Observable<boolean>是一个可观察的布尔值(布尔值流)。 An Observable<string> is an Observable of strings (a stream of string values) and so on. Observable<string>Observable<string>的 Observable(字符串值流)等等。

Read about Generic types here: https://www.typescriptlang.org/docs/handbook/2/generics.html在此处阅读泛型类型: https : //www.typescriptlang.org/docs/handbook/2/generics.html

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

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