【问题标题】:TypeScript doesn’t recognize .d.ts from node moduleTypeScript 无法识别来自节点模块的 .d.ts
【发布时间】:2019-05-09 15:43:25
【问题描述】:

我正在使用 babel-plugin-react-intl-auto 和 TypeScript。

这个包在lib/types.d.ts 中定义了类型(使用 npm 安装时它在 lib 中)。这些覆盖另一个包的类型。

import { FormattedMessage } from 'react-intl'

declare module 'react-intl' {
  interface ExtractableMessage {
    [key: string]: string
  }

  export function defineMessages<T extends ExtractableMessage>(
    messages: T
  ): { [K in keyof T]: FormattedMessage.MessageDescriptor }
}

他们在package.json中指定了这个

{
  // ...
  "types": "lib/types.d.ts",
  // ...
}

这一切似乎都很好。但是,当我使用这个包时,TypeScript 抱怨defineMessages 的函数签名。

如果我将这些类型复制到我自己的 index.d.ts 中,它会按预期工作。

为了完整起见,这是我的 tsconfig.json

{
  "compilerOptions": {
    "noEmit": true,
    "noImplicitAny": true,
    "esModuleInterop": true,
    "jsx": "react",
    "lib": ["es5", "es6", "dom"]
  }
}

为什么 TypeScript 不使用这些类型?

【问题讨论】:

    标签: typescript


    【解决方案1】:

    这很可能是因为 TypeScript 没有关于使用哪个版本的 defineMessages 的上下文。
    TypeScript 支持merging declarations,这在导入模块时也会应用。

    babel-plugin-react-intl-autoreact-intl 一起导入应该合并这些定义,让TypeScript 能够理解这两种语法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-13
      • 1970-01-01
      • 1970-01-01
      • 2021-02-28
      • 2019-04-07
      • 2019-04-10
      • 2016-02-25
      相关资源
      最近更新 更多