JE42 JE42 - 1 month ago 6x
TypeScript Question

What what does it mean when import is followed by a require?

The raven documentation for Angular 2 mentions the following es6 code snippet:

import Raven = require('raven-js'); // NOTE: "require" not "from"

They explicitly mentioned the require however I am not sure what this statement actually does and where it is documented.

import Raven = require('raven-js'); 

is the original Typescript import syntax.

The new ES6 style import is now preferred (since TS 1.5)

import * as Raven from 'raven-js'

These two syntaxes import an external library/module for which type definitions must be available, either as separate definition files referenced in tsconfig.json or as part of the libray with a reference to them in the typings entry of its package.json (the ///<reference syntax should be avoided).

Please note that, in a 'require' environment, such as commonjs,

const Raven = require('raven-js'); 

will make Raven available to the rest of the code as any (definitions will not be imported). This may be handy when trying to use a JS library for which you do not have (or do do want to craft) the definitions.