Skip to content

Exporting and importing modules

Course: Typescript - chapter 11

TypeScript follows the ES6 module system, using **import** and **export** statements to share code between different files. Here’s a brief overview of how this works:

math.ts

export function add(x: number, y: number): number {
return x + y;
}
export function subtract(x: number, y: number): number {
return x - y;
}

main.ts

import { add } from "./math"
add(1, 2)
export default class Calculator {
add(x: number, y: number): number {
return x + y;
}
}

calculator.ts

import Calculator from './Calculator';
const calc = new Calculator();
console.log(calc.add(10, 5));