Remco Haszing
Remco Haszing

Reputation: 7829

TypeScript doesn’t recognize .d.ts from node module

I’m using babel-plugin-react-intl-auto and TypeScript.

This package has defined typings in lib/types.d.ts (It is in lib when installed using npm). These override typings of another package.

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 }
}

They have specified this in package.json

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

This all seems fine. However, When I use this package, TypeScript complains about the function signature of defineMessages.

If I copy those typings into my own index.d.ts, it just works as expected.

For completness sake, this is my tsconfig.json:

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

Why won’t TypeScript use these typings?

Upvotes: 0

Views: 899

Answers (1)

W. K.
W. K.

Reputation: 26

This is most likely because TypeScript has no context as to which version of defineMessages to use.
TypeScript supports merging declarations which also gets applied when importing modules.

Importing babel-plugin-react-intl-auto alongside react-intl should merge these definitions, allowing TypeScript to understand both syntaxes.

Upvotes: 1

Related Questions