[英]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.