[英]What is a type extends in a class in Typescript?
Please can you clarify what is the meaning of this notation in Typescript and where to find documentation?请您澄清一下 Typescript 中这个符号的含义以及在哪里可以找到文档? Is typeA an alias of typeB?
typeA 是 typeB 的别名吗? Does typeA extends also typeC and typeD?
typeA 是否也扩展了 typeC 和 typeD? Does myClass "see" typeA inside itself?
myClass 是否“看到” typeA 内部? Thanks.
谢谢。
classe myClass<
typeA extends typeB = Unknown
> extends anotherClass<
typeC,
typeD
>
What you are seeing here is a generic class which extends another generic class.您在这里看到的是通用 class ,它扩展了另一个通用 class。
typeB
, typeC
, typeC
, and Unknown
are all known, pre-defined types. typeB
、 typeC
、 typeC
和Unknown
都是已知的预定义类型。 typeA
is a variable. typeA
是一个变量。 (The example could make that distinction a lot more clear.) (这个例子可以使这种区别更加清晰。)
Does myClass "see" typeA inside itself?
myClass 是否“看到” typeA 内部?
Yes!是的!
myClass
can use the type typeA
for any of its methods or other internal typings. myClass
可以将typeA
类型用于其任何方法或其他内部类型。
Does typeA extends also typeC and typeD?
typeA 是否也扩展了 typeC 和 typeD?
No. It might be true in some cases but it's not required.不,在某些情况下可能是这样,但不是必需的。
anotherClass
has two generic variables. anotherClass
有两个泛型变量。 We are stating that our class myClass
extends the version of anotherClass
where those variables are set to typeC
and typeD
.我们声明我们的 class
myClass
扩展了anotherClass
的版本,其中这些变量设置为typeC
和typeD
。 Again, typeC
and typeD
are known types.同样,
typeC
和typeD
是已知类型。
Is typeA an alias of typeB?
typeA 是 typeB 的别名吗?
No, not always.不,并非总是如此。 Our variable
typeA
must extend
the known type typeB
.我们的变量
typeA
必须extend
已知类型typeB
。 It could be typeB
exactly or something that extends it.它可能完全是
typeB
或扩展它的东西。 It is ok to use typeA
anywhere that requires typeB
since we know that typeA extends typeB
.可以在任何需要
typeB
的地方使用typeA
,因为我们知道typeA extends typeB
。
Unknown
is the fallback value for the variable typeA
is no type can be inferred. Unknown
是变量typeA
的后备值,无法推断出任何类型。 Unknown
must also extend typeB
. Unknown
还必须扩展typeB
。
Hopefully this example makes it more clear:希望这个例子能更清楚地说明:
// AnotherClass depends on two generics which could be anything
class AnotherClass<DataType, KeyType> {
// the generics are used here to determine the types of the instance variables
data: DataType;
key: KeyType;
// the generic values for each instance will be determined by the arguments which are passed to the constructor
constructor(data: DataType, key: KeyType) {
this.data = data;
this.key = key;
}
}
// MyInterface is some sort of known type
interface MyInterface {
myKey: string;
}
// MyClass has one generics value Name which can be a specific string or just `string`
// it extends AnotherClass where `DataType` is `MyInterface` and `KeyType` is `string`
class MyClass<Name extends string = string> extends AnotherClass<MyInterface, string> {
name: Name;
constructor(name: Name, data: MyInterface) {
// must call superclass AnotherClass's constructor
super(data, name);
this.name = name;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.