将本地类型用于npm软件包,而不将其发布到@types

时间:2019-04-15 13:34:33

标签: reactjs typescript

我想使用此npm软件包(https://www.npmjs.com/package/react-web-notification),所以我在node_modules / @ types / react-web-notification文件夹中创建了一个index.d.ts文件:

import * as React from 'react';

export interface propTypes {
    ignore?: bool,
    disableActiveWindow?: bool,
    askAgain?: bool,
    notSupported?: func,
    onPermissionGranted?: func,
    onPermissionDenied?: func,
    onShow?: func,
    onClick?: func,
    onClose?: func,
    onError?: func,
    timeout?: number,
    title?: string.isRequired,
    options?: object,
    swRegistration?: object,
};

declare class Notification extends React.Component<propTypes, any> {}

export default Notification

,它工作正常。但显然我不想将其保留在那里,因为它被忽略了。我不想将其发布到@types github存储库,因为克隆该存储库需要花费很多时间。

因此,我尝试在我的react app src文件夹中本地使用它: NotificationInterface.tsx:

    import * as React from 'react';

interface propTypes {
    ignore?: boolean,
    disableActiveWindow?: boolean,
    askAgain?: boolean,
    notSupported?: Function,
    onPermissionGranted?: Function,
    onPermissionDenied?: Function,
    onShow?: Function,
    onClick?: Function,
    onClose?: Function,
    onError?: Function,
    timeout?: number,
    title?: string,
    options?: object,
    swRegistration?: object,
}

declare class Notification extends React.Component<propTypes, any> {}

export default Notification

,然后在我的应用中:

import Notification from "../NotificationInterface";
...

    return (
             <main>
                    <Notification
                        ignore={this.state.ignore && this.state.title !== ''}
                        onPermissionGranted={this.handlePermissionGranted}
                        onPermissionDenied={this.handlePermissionDenied}
                        onShow={this.handleNotificationOnShow}
                        onClick={this.handleNotificationOnClick}
                        onClose={this.handleNotificationOnClose}
                        onError={this.handleNotificationOnError}
                        timeout={60000}
                        title={this.state.title}
                        options={this.state.options}
                    />

但是现在我得到一个错误: 无法构造“通知”:请使用“ new”运算符,此DOM对象构造函数不能作为函数调用。

为什么它在@types文件夹中而不是现在不起作用?

1 个答案:

答案 0 :(得分:1)

因此,这是向未键入模块提供键入内容的方法。添加具有以下内容的文件并将其包含在项目文件中:

declare module 'react-web-notification' {
    import * as React from 'react';

    interface Props {
        ignore?: boolean,
        disableActiveWindow?: boolean,
        askAgain?: boolean,
        notSupported?: Function,
        onPermissionGranted?: Function,
        onPermissionDenied?: Function,
        onShow?: Function,
        onClick?: Function,
        onClose?: Function,
        onError?: Function,
        timeout?: number,
        title: string,
        options?: object,
        swRegistration?: object,
    }
    class Notification extends React.Component<Props> {}

    export = Notification;
}

请注意模块声明。

旁注-您可以将声明添加到https://github.com/DefinitelyTyped/DefinitelyTyped中,发布后,您就可以“常规”方式使用它们了。