【问题标题】:How do you monkey-patch a Definitively-Typed definition你如何猴子补丁一个明确类型的定义
【发布时间】:2017-08-18 10:48:21
【问题描述】:

我正在使用一个 npm 模块,在 @types/*/index.d.ts 文件中缺少一些定义。 我已经提交了关于它们的问题。

与此同时,我想在本地做一些事情来解决这个问题,但事实证明这很困难。

我尝试只修补 index.d.ts 文件,但我使用的是 yarn,每次我添加另一个模块时,它都会嗅出更改并“修复”文件。所以最好有某种本地补丁。

我尝试添加本地定义,但要么我做得不对,要么不可能。这是许多尝试中的一种:本地定义,例如

import {MapControl} from 'react-leaflet'
...
declare class MapControl extends React.Component<MapControlProps, any> {
    leafletElement: L.Control
}

没有帮助; import 没有参考 MapControl 的实现,只是仍然看到没有定义。直接指向node_modules/react-leaflet/...只会遇到其他问题。

【问题讨论】:

    标签: typescript typescript-typings


    【解决方案1】:

    您的模块扩充语法需要:

    import * as React from "React";
    
    declare module 'react-leaflet' {
        type MapControlPosition = 'topleft' | 'topright' | 'bottomleft' | 'bottomright';
    
        interface MapControlProps {
            position: MapControlPosition
        }
    
        class MapControl extends React.Component<MapControlProps, any> {
            leafletElement: L.Control
        }
    }
    

    更多详情请看这里https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-8.html#augmenting-globalmodule-scope-from-modules

    【讨论】:

      猜你喜欢
      • 2011-04-15
      • 1970-01-01
      • 2012-08-19
      • 2011-10-06
      • 2020-05-24
      • 2016-10-01
      • 2016-09-01
      • 2012-09-16
      • 2012-12-18
      相关资源
      最近更新 更多