Modules
Both modern JS and TypeScript support modules. Each file is a module, than can export some properties:
1
// src/foo.ts
2
export const foo = 12
Copied!
Which then are imported in another file:
1
// src/bar.ts
2
import { foo } from "./foo"
Copied!
Each file can also have a default export, which means you don't have to name the exported property:
1
// src/baz.ts
2
export default 42;
Copied!
You can name it when importing:
1
// src/bar.ts
2
import Baz from "./baz";
Copied!
You can also mix both styles:
1
import DefaultExport, { namedExport1, namedExport2 } from "./a-file"
Copied!

Importing npm modules

Importing code installed via npm is done by specifying the package name instead of relative path:
1
import React from "react"
Copied!
Some (usually older) packages have no default export and have to be imported as a group of properties;
1
import * as _ from "underscore"
Copied!

Resources