Modules

Both modern JS and TypeScript support modules. Each file is a module, than can export some properties:

// src/foo.ts
export const foo = 12

Which then are imported in another file:

// src/bar.ts
import { foo } from "./foo"

Each file can also have a default export, which means you don't have to name the exported property:

// src/baz.ts
export default 42;

You can name it when importing:

// src/bar.ts
import Baz from "./baz";

You can also mix both styles:

import DefaultExport, { namedExport1, namedExport2 } from "./a-file"

Importing npm modules

Importing code installed via npm is done by specifying the package name instead of relative path:

import React from "react"

Some (usually older) packages have no default export and have to be imported as a group of properties;

import * as _ from "underscore"

Resources

Last updated