【问题标题】:Overriding TypeScript definitions extended by other libraries覆盖由其他库扩展的 TypeScript 定义
【发布时间】:2016-08-15 18:00:27
【问题描述】:

我遇到过几种情况,其中一个库扩展了另一个库,尤其是在我使用 React 和 Redux 时。

例如,假设我正在使用一个 JS 库,该库导出了一个可以这样定义的函数:

function dispatch(action:IAction):void;

interface IAction {
    type: string;
}

我使用另一个 JS 库来增强 dispatch 函数以允许回调,可以这样定义:

function dispatch(action:IAction | IActionCallback):void;

interface IActionCallback {
    (dispatch:IDispatch):void;
}

问题是第二个库扩充了第一个库。这如何在打字中正确表达?或者可以吗?

【问题讨论】:

    标签: typescript typescript-typings


    【解决方案1】:

    所以你要做的是让扩展库的声明文件“增强”原始库。

    redux-thunk declaration file 几乎做到了这一点:它增强了 redux 的 Dispatch 接口。

    如需更多信息,I wrote a more detailed answer 将了解如果您想扩充另一个模块的不同可能性,具体取决于您的声明文件是否需要是全局的。或者看看打字稿手册中的page on declaration merging

    如果您还有其他问题,我很乐意为您提供帮助 ?。

    【讨论】:

    • 谢谢,仍在尝试消化这个......我在这里遇到的 Redux Thunk 问题是,如果我 import {IDispatch} from "redux" 它似乎不知道允许的 redux-thunk 增强功能成为动作。它只有在我 import {IDispatch} from "~redux-thunk~redux" 时才有效,这很奇怪,我不确定导入某些东西的正确方法,因为那不是真正的模块。
    猜你喜欢
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    • 2013-10-12
    • 1970-01-01
    • 2023-01-04
    • 2020-12-22
    相关资源
    最近更新 更多