Could someone please explain exactly how exports and imports work in NodeJS using Typescript?
My setup is:
I am messing about with exports/imports instead of doing some proper coding, its driving me nuts, and cannot find any proper explanation of how it works.
Import
Can you please explain following:
var module = require ("module"); import module = require("module"); import module from "module"; import {something} from "module"; import * as module from "module";
Export
Can you please explain following
export = something; export default something; export interface|class something;
Questions
I cannot seem to find proper way of doing exports vs. imports so my IDE is not covered in red and throwing hundreds of errors at me.
General Questions
List of questions goes on and on, but im sure once some of the above is answered, I can pick up on the rest.
Thank you and sorry for such a general question, but my frustration level just reached the top...
The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum. When exporting a module using export = , TypeScript-specific import module = require("module") must be used to import the module.
A module can be created using the keyword export and a module can be used in another module using the keyword import . In TypeScript, files containing a top-level export or import are considered modules. For example, we can make the above files as modules as below. console.
Use import myFunction from "./myModule" to bring it in. More commonly, TypeScript modules say export myFunction in which case myFunction will be one of the properties on the exported object. Use import { myFunction } from "./myModule" to bring it in.
Agreed, import/export syntax is confusing for at least two reasons:
var module = require ("module");
works but that is commonjs -> no typingsimport x = require('y'
) is now deprecated in TSTL;DR;: Use the 'es6 style' syntax introduced in TS 1.5
The 'best' resource on import/export in TS I know is this
Overall I recommend reading this excellent handbook which will provide answers to most of your questions
From a default export
Something
was exported as a default (single) export ie export default Something
in ts/es6
Use
import Something from "module"
You can actually import a default export with a different name. import SomethingElse from 'module'
will also work
From named exports
Something
was exported as a named export in "module" using export {Something}
or export class|interface Something{}
in ts/es6
You want to import only that, use
import {Something} from "module"
You want to import everything that is exported from "module" under the namespace mod
import * as mod from "module
Then use const c:mod.Something = whatever
See import
s above
The form export = something
is deprecated in favor of the new ES6 style syntax. It is mostly found in definition files to express the fact that a js library exports a single function/object e.g. module.exports=something
.
Use ES6 style syntax and avoid default
exports: they have the advantage that they can be imported using a different name but
import {Something as SomethingElse} from "module"
Concretely, export whatever needs to be exported and import it specifically
In api.ts
export interface MyInterface { } export class MyClass { }
In main.ts
import {MyInterface, MyClass} from './api'
There are a lot of good IDEs out there that provide excellent linting: VSCode, Atom Typescript and Webstorm to name a popular few, the first two being free and the third one even manages the imports for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With