【问题标题】:TypeScript 2.0 and merging the react-router interface declared in Route.d.tsTypeScript 2.0 和合并在 Route.d.ts 中声明的 react-router 接口
【发布时间】:2017-02-13 00:48:14
【问题描述】:

我想在 RouteProps 接口中添加一个名称属性。没有它我得到:

错误 TS2339:“IntrinsicAttributes & > IntrinsicClassAttributes> & RouteProp...”类型上不存在属性“名称”

如果我将 name?: string; 添加到外部 Route.d.ts 文件中,错误就会消失。

我尝试了多种建议的方式合并模块,但均未成功。

外部 Route.d.ts 声明的开头是这样的:

declare const self: self.Route;
type self = self.Route;
export default self;

declare namespace self {

    interface RouteProps extends React.Props<Route> {...

我正在使用 TypeScript 2。

【问题讨论】:

    标签: typescript react-router typescript2.0


    【解决方案1】:

    我有同样的问题,我使用“模块增强”:https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation

    您可以添加一个新文件 custom-route.d.ts 并编写代码

    declare module "react-router/lib/Route" {
        interface RouteProps {
            name?: String;
        }
    }
    

    你可以看到我的问题 Typescript custom @types package for @types/react-router

    【讨论】:

    • 请从您提供的链接中添加一些重要部分/不欢迎仅链接的答案。 :)
    猜你喜欢
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    • 2021-01-20
    相关资源
    最近更新 更多