Import syntax lets you bring code from other files or libraries into your current file. This helps you reuse code and keep your project organized.
Import syntax variations in Typescript
import defaultExport from 'module-name'; import * as name from 'module-name'; import { export1, export2 } from 'module-name'; import { export1 as alias1 } from 'module-name'; import defaultExport, { export1, export2 } from 'module-name'; import 'module-name';
Use single quotes or double quotes for module names.
Default export means the main thing exported from a module.
import React from 'react';
import * as Utils from './utils';
import { useState, useEffect } from 'react';
import { useState as useLocalState } from 'react';
This program imports two functions, sqrt and pow, from the 'mathjs' library. It calculates the square root and the square of a number and prints the results.
import { sqrt, pow } from 'mathjs'; const number = 16; const root = sqrt(number); const power = pow(number, 2); console.log(`Square root of ${number} is ${root}`); console.log(`${number} raised to power 2 is ${power}`);
Always check if the module you want to import from has a default export or named exports.
You can combine default and named imports in one statement.
Importing a module without bindings (import 'module-name';) runs the module code but does not import anything.
Import syntax helps you bring code from other files or libraries into your file.
You can import default exports, named exports, or everything as an object.
Renaming imports helps avoid name conflicts in your code.
Practice
utils?Solution
Step 1: Understand default export import syntax
The default export is imported without curly braces usingimport name from 'module'.Step 2: Analyze each option
import utils from './utils'; uses the correct syntax for default import. import { utils } from './utils'; uses curly braces which is for named exports. import * as utils from './utils'; imports everything as an object, not default. import utils = require('./utils'); is CommonJS style, not ES module syntax.Final Answer:
import utils from './utils'; -> Option AQuick Check:
Default import = import name from 'module' [OK]
- Using curly braces for default imports
- Confusing * as import with default import
- Mixing CommonJS require syntax with ES modules
calculate from a module mathUtils?Solution
Step 1: Identify named export import syntax
Named exports are imported using curly braces:import { name } from 'module'.Step 2: Check each option
import { calculate } from './mathUtils'; correctly imports the named exportcalculate. import calculate from './mathUtils'; is for default imports. import * as calculate from './mathUtils'; imports all exports as an object, not a single named export. import calculate = require('./mathUtils'); uses CommonJS syntax, not ES modules.Final Answer:
import { calculate } from './mathUtils'; -> Option BQuick Check:
Named import = import { name } from 'module' [OK]
- Omitting curly braces for named imports
- Using * as import when only one named export is needed
- Confusing default and named import syntax
import * as helpers from './helpers'; console.log(typeof helpers); console.log(typeof helpers.formatDate);
Assume
helpers exports a function named formatDate.Solution
Step 1: Understand
Usingimport * assyntaximport * as helpersimports all exports as an object namedhelpers.Step 2: Determine types of
helpersandhelpers.formatDatehelpersis an object, sotypeof helpersis "object".formatDateis a function exported insidehelpers, sotypeof helpers.formatDateis "function".Final Answer:
"object" and "function" -> Option CQuick Check:
import * as = object, function inside = function [OK]
- Thinking import * as imports a function directly
- Confusing typeof results for functions and objects
- Assuming default export with import * as syntax
import { default as myFunc } from './module';Solution
Step 1: Understand default import syntax
Default imports are imported without curly braces and can be renamed directly:import myFunc from 'module'.Step 2: Analyze the given syntax
The syntaximport { default as myFunc }tries to rename the default export inside curly braces, which is invalid in ES module syntax.Final Answer:
You cannot rename the default import using curly braces. -> Option AQuick Check:
Default import no braces, no rename inside braces [OK]
- Trying to rename default import inside curly braces
- Confusing named export renaming with default import
- Assuming default import can be destructured
parseData from dataUtils but avoid naming conflicts by renaming it to parse. Which is the correct import statement?Solution
Step 1: Understand renaming named imports
You can rename named imports usingimport { originalName as newName }.Step 2: Check each option
import { parseData as parse } from './dataUtils'; correctly renamesparseDatatoparse. import parse from './dataUtils'; imports default export, not named. import * as parse from './dataUtils'; imports all exports as an object namedparse. import { parse as parseData } from './dataUtils'; tries to renameparsetoparseData, which is reversed.Final Answer:
import { parseData as parse } from './dataUtils'; -> Option DQuick Check:
Rename named import with 'as' inside braces [OK]
- Using default import syntax for named exports
- Confusing rename direction in 'as' syntax
- Using * as when only one function is needed
Ready to prove it?
Pass the test and this pattern moves unknown
