Avraam Mavridis Avraam Mavridis - 10 days ago 8
Javascript Question

Install npm package that does not have typings

I am using Typescript 2.1. I am trying to install an npm package that doesn't have typings (

reactable
).

If I do:

import * as Reactable from 'reactable'
typescript complains that cannot find this module.

If I do:

const Reactable = require('reactable')
typescript complains that
require
is forbidden.

If I do the weird syntax that suggested in another post:

import Reactable = require('reactable');
typescript complains that
import
assignment cannot be used to target es2015 modules.

Is there any way to import npm packages that do not have typings?

Answer

You'll need to declare something about what you're using. For example

declare module "reactable" {
    interface ReactableProps {
        ....
    }

    interface ReactableState {
        ....
    }

    class reactable extends React.Component< ReactableProps, ReactableState> { }
}

You can start with just what you're using and need the IDE to support, and slowly add things up.

You can check the Writing Declaration Files for more info on how to do so.