typescript - 你如何猴子修补明确类型的定义

标签 typescript typescript-typings

我使用的 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/... 只会遇到其他问题。

最佳答案

模块扩充的语法需要是:

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

关于typescript - 你如何猴子修补明确类型的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43017430/

相关文章:

javascript - 是否有必要为 typescript 中使用的每个 JS 库提供类型/定义文件?

node.js - TS2300 : Duplicate Identifier when moved to Angular2 RC2

node.js - 如何在Node.js和TypeScript项目中使用URLSearchParams

javascript - 如何为 createReadStream 分配缓冲区

javascript - 元组与硬编码字符串

javascript - 自定义重试功能

javascript - 组件初始化后更新父组件@Input属性

typescript - 获取通过另一个字段指定的泛型属性值的类型

typescript - 尝试对新的明确类型定义运行 lint 时出错

d3.js - Typescript 期望从 d3.nest().rollup() 中的辅助函数返回 undefined