参数列表前的“new”关键字在打字稿箭头函数中是什么意思?

就此而言,我对 TypeScript 甚至 JavaScript 还很陌生。我一直在尝试围绕 Microsoft 的一个示例,介绍如何将 AzureAD 身份验证集成到 React 应用程序中。该示例使用 HOC 为组件提供身份验证。HOC 的声明如下所示:

function withAuthProvider<T extends React.Component<AuthComponentProps>>(
    WrappedComponent: new (props: AuthComponentProps, context?: any) => T
): React.ComponentClass {...}

其中大部分或多或少是清楚的。令我困惑的是WrappedComponent. 具体来说,我不明白new关键字在该上下文中的作用。

谁能帮我吗?

回答

它是一个构造函数类型。这意味着当你用 调用它时new,你可以给它一个props参数和一个可选context参数,它会构造一个 type 的实例T

下面是一个例子:

class Foo {
    private value: number;
    constructor(x: number, y: number = 1) {
        this.value = x + y;
    }
}

const foo: new (arg1: number, arg2?: number) => Foo = Foo;
// can be invoked like this (with new)
const x1: Foo = new foo(1);
const x2: Foo = new foo(1, 2);
// cannot be invoked without new
// these lines will error at both compile- and run-time
const y1: Foo = foo(1);
const y2: Foo = foo(1, 2);


以上是参数列表前的“new”关键字在打字稿箭头函数中是什么意思?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>