Ankit Raonka
Ankit Raonka

Reputation: 6859

Is it necessary to have typing/definiton files for every JS library you use in typescript?

Is it necessary to have typing for every JS library you use in typescript? if not, then how to get rid of errors and make use of a library which doesn't have available definition files

import { NotificationContainer, NotificationManager } from 'react-notifications';

can't find typings for react-notifications

Upvotes: 0

Views: 330

Answers (2)

Bruno Grieder
Bruno Grieder

Reputation: 29824

An alternative to @Daniel Earwicker answer, as long as you are in a commonJS environment (and I guess you are using webpack) is to simply require the library using node require

const rn = require('react-notifications')

then use rn.NotificationContainer directly or import NotificationContainer = rn.NotificationContainer

Upvotes: 1

Daniel Earwicker
Daniel Earwicker

Reputation: 116674

It's not necessary. For your example, you create a file react-notifications.d.ts (you can call it anything you like as long as the extension is .d.ts but it makes sense to name it consistently):

declare module "react-notifications" {

    const NotificationContainer: any;
    const NotificationManager: any;
}

That's a near-minimal starting point. But you could go a little further and improve on those any declarations. They are a temporary cop-out.

Upvotes: 2

Related Questions