Exporting lets you share code parts so other files can use them. It helps keep code organized and reusable.
Export syntax variations in Typescript
Introduction
Syntax
Typescript
export const name = value; export function func() {} export class MyClass {} const localVar = 5; export { localVar }; export { localVar as renamedVar }; export default function() {} export { something } from './otherFile';
export keyword makes code available outside the file.
default export means one main thing is exported from the file.
Examples
pi so other files can use it.Typescript
export const pi = 3.14;
greet.Typescript
export function greet() { console.log('Hello!'); }
secret but renames it to answer for outside use.Typescript
const secret = 42; export { secret as answer };
Person. Only one default export per file.Typescript
export default class Person { constructor(name: string) { this.name = name; } }
Sample Program
This example shows different export styles in mathUtils.ts. The main.ts file imports them all and uses them.
Typescript
// file: mathUtils.ts export const add = (a: number, b: number) => a + b; export const subtract = (a: number, b: number) => a - b; const secretNumber = 7; export { secretNumber as luckyNumber }; export default function multiply(a: number, b: number) { return a * b; } // file: main.ts import multiply, { add, subtract, luckyNumber } from './mathUtils'; console.log(add(2, 3)); console.log(subtract(5, 1)); console.log(multiply(4, 6)); console.log(luckyNumber);
Important Notes
You can have many named exports but only one default export per file.
When importing default exports, you can choose any name you like.
Renaming exports helps avoid name clashes or clarify meaning.
Summary
Use export to share variables, functions, or classes.
Named exports let you export many things; default export is for one main thing.
You can rename exports to keep code clear and avoid conflicts.
Practice
1. Which of the following is the correct way to export a function named
calculate as a named export in TypeScript?easy
Solution
Step 1: Understand named export syntax
Named exports use the keywordexportbefore the declaration to share multiple items.Step 2: Identify correct syntax for named function export
The syntaxexport function calculate() {}correctly exports the function as named export.Final Answer:
export function calculate() { /*...*/ } -> Option DQuick Check:
Named export usesexportbefore declaration [OK]
Hint: Use 'export' before function for named export [OK]
Common Mistakes:
- Confusing default export with named export
- Using CommonJS syntax like module.exports
- Using 'export =' which is for different module style
2. Which of the following is the correct syntax to export a constant named
PI as the default export in TypeScript?easy
Solution
Step 1: Understand default export syntax
Default export can export a value or variable by writingexport defaultfollowed by the value or variable.Step 2: Identify correct way to export constant as default
We first declareconst PI = 3.14;then export it as default usingexport default PI;.Final Answer:
const PI = 3.14; export default PI; -> Option BQuick Check:
Default export uses 'export default' after declaration [OK]
Hint: Declare first, then export default variable [OK]
Common Mistakes:
- Trying to combine 'const' and 'default' in one statement incorrectly
- Using 'export const default' which is invalid
- Confusing named export with default export syntax
3. What will be the output of the following TypeScript code?
export const a = 5; export const b = 10; import * as values from './module'; console.log(values.a + values.b);
medium
Solution
Step 1: Understand named exports and import * syntax
The code exports two constantsaandbas named exports. The import statement imports all exports as an object namedvalues.Step 2: Calculate the sum of imported values
Accessingvalues.aandvalues.bgives 5 and 10 respectively. Their sum is 15.Final Answer:
15 -> Option AQuick Check:
Named exports imported as object sum correctly [OK]
Hint: Import * as object to access all named exports [OK]
Common Mistakes:
- Expecting default export when none is present
- Confusing import syntax causing runtime errors
- Assuming imported values are undefined
4. Identify the error in the following TypeScript export code:
const name = "Alice";
export { name as default };medium
Solution
Step 1: Understand export renaming rules
Renaming exports is allowed, but renaming a named export todefaultusing this syntax is invalid in TypeScript.Step 2: Identify correct way to export default with rename
To export a variable as default, useexport default name;instead of renaming in braces.Final Answer:
You cannot rename a named export to default in this way. -> Option CQuick Check:
Default export must use 'export default' syntax [OK]
Hint: Use 'export default' for default export, not rename [OK]
Common Mistakes:
- Trying to rename named export to default inside braces
- Confusing export syntax for default and named exports
- Assuming semicolon omission causes error
5. Given the following module code:
Which import statement correctly imports
const x = 1;
const y = 2;
const z = 3;
export { x as default, y, z };Which import statement correctly imports
x as default and y and z as named imports?hard
Solution
Step 1: Understand export renaming and import syntax
The module exportsxrenamed as default, andy,zas named exports.Step 2: Identify correct import syntax for default and named exports
To import default export asxand named exportsy,z, useimport x, { y, z } from './module';.Final Answer:
import x, { y, z } from './module'; -> Option AQuick Check:
Default import first, then named in braces [OK]
Hint: Default import first, named imports in braces [OK]
Common Mistakes:
- Using named import syntax for default export
- Trying to import default as named
- Using import * without destructuring
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
