繁体   English   中英

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

[英]Use your local types for an npm package without publishing it to @types

我想使用这个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文件夹中而不是现在不起作用?

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

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 ,一旦发布,您就可以以“常规”方式使用它们

暂无
暂无

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

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