typescript
  • A crash guide to Typescript
  • Intro
    • What Typescript IS for?
    • What Typescript is NOT for?
    • Setting up tsconfig.json
  • Types are spooky! (How types work?)
  • Handling mutable code
  • The primitive types
  • Interfaces or Type Aliases? Oh, my!
  • The `class`, a creature which spans both realms
  • Structural typing
  • Control Flow Analysis
  • More advanced type syntaxes for another day
  • Generics
  • Modules
  • 3rd-party types
  • Epilogue
Powered by GitBook
On this page
  • Module-less typescript
  • Moduleful typescript

Was this helpful?

Modules

TypeScript is made to adapt to JavaScript. And JavaScript itself has had many module systems for different environments and times. Most notably:

  • The browser console "vanilla" environment is module-less. Every imported file lives in the global scope;

  • node.js traditionally uses the "commonjs" module syntax;

  • Modern front-end code built with module bundlers usually use the "es-modules" syntax;

Module-less typescript

  • A TypeScript file is considered module-less if it has no imports or exports;

  • All typescript source files share the same global context. Which is defined at the include entry of the tsconfig;

  • A file can manually include a reference through the addition of the "triple slash directive" at the first line.

///<reference path=“./path/to/file”/>

Moduleful typescript

  • The TS import syntax comes from the es-module syntax;

  • You can also write some additional syntax not covered by the es-modules:

import express = require("express") // enforce commonjs import
const express = require("express")  // this works BUT 3rd party types won't get imported
import * as express from 'express'
import express from 'express' // only works with "esModuleInterop"
export = { something: 'x' } // "module.exports =" syntax from commonjs
PreviousGenericsNext3rd-party types

Last updated 3 years ago

Was this helpful?