JE42
JE42

Reputation: 5149

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.

Upvotes: 2

Views: 650

Answers (2)

Bruno Grieder
Bruno Grieder

Reputation: 29814

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 library 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 (i.e. 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.

Upvotes: 1

Saad
Saad

Reputation: 53799

The import with require comes from TypeScript's own module system. You can read about it in depth here.

It also mentions how to use different types of module syntaxes (CommonJS, Native ES6 Modules, etc)

Upvotes: 0

Related Questions