简体   繁体   English

typescript 中的语法是什么?

[英]What is this syntax in typescript?

    type JSXElementConstructor<P> =
        | ((props: P) => ReactElement<any, any> | null)
        | (new (props: P) => Component<any, any>);       => What is this?

It seems it's trying to define a method signature but then what how is it different from the way the line above describes a method?它似乎试图定义一个方法签名,但它与上面一行描述方法的方式有何不同?

((props: P) => ReactElement<any, any> | null) describes a plain function that, when called, returns a ReactElement. ((props: P) => ReactElement<any, any> | null)描述了一个简单的 function,当调用它时,返回一个 ReactElement。

(new (props: P) => Component<any, any>) describes a class that, when called with new , returns a Component - hence the new . (new (props: P) => Component<any, any>)描述了一个class ,当用new调用时,返回一个 Component - 因此是new

They're not interchangeable.它们不可互换。 The union lets JSXElementConstructor account for both functional components and class components.联合让JSXElementConstructor考虑功能组件和 class 组件。

For a simple example outside of React of a newable that's assignable to new () => :对于一个可分配给new () =>的 newable 的 React 之外的简单示例:

type X = (new () => { getName: () => string });
class Person {
    #name = 'foo';
    getName() {
        return this.#name;
    }
}

const P: X = Person;

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

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