简体   繁体   English

在 Typescript 中的 class 中扩展的类型是什么?

[英]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. typeBtypeCtypeCUnknown都是已知的预定义类型。 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的版本,其中这些变量设置为typeCtypeD Again, typeC and typeD are known types.同样, typeCtypeD是已知类型。

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;
    }
}

Typescript Playground Link Typescript 游乐场链接

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

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