Bird
Raised Fist0
Typescriptprogramming~10 mins

Import syntax variations in Typescript - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Import syntax variations
Start
↓
Choose import type
↓
Default
↓
Import default
↓
Use imported
↓
End
This flow shows how TypeScript imports can be default, named, or namespace style, then used in code.
Execution Sample
Typescript
import React from 'react';
import { useState } from 'react';
import * as Utils from './utils';
This code imports React as default, useState as named, and all exports from utils as namespace.
Execution Table
StepImport StatementImport TypeImported IdentifierSource ModuleUsage Example
1import React from 'react';DefaultReact'react'React.createElement(...)
2import { useState } from 'react';NameduseState'react'const [state, setState] = useState()
3import * as Utils from './utils';NamespaceUtils'./utils'Utils.helperFunction()
4End of imports----
💡 All import statements processed, ready to use imported identifiers.
Variable Tracker
IdentifierStartAfter Import
Reactundefinedobject (default export from 'react')
useStateundefinedfunction (named export from 'react')
Utilsundefinedobject (all exports from './utils')
Key Moments - 3 Insights
Why can't I use curly braces with default imports?
Default imports do not use curly braces because they import the single default export. Named imports use curly braces as shown in step 2 of the execution_table.
What does the * as alias syntax do?
It imports all exports from the module as properties of the alias object, shown in step 3 of the execution_table where Utils is the alias.
Can I mix default and named imports in one statement?
Yes, you can combine them like: import React, { useState } from 'react'; but each import type has its own syntax as shown in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what type of import is used for 'useState'?
ADefault import
BNamed import
CNamespace import
DSide-effect import
💡 Hint
Check the 'Import Type' column for step 2 in the execution_table.
At which step does the code import everything from a module as an object?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the 'Namespace' import type in the execution_table.
If you want to import only the default export and a named export together, how would the import statement look?
Aimport React, { useState } from 'react';
Bimport { React, useState } from 'react';
Cimport React from 'react';
Dimport * as React from 'react';
💡 Hint
Refer to key_moments explanation about mixing default and named imports.
Concept Snapshot
Import syntax variations in TypeScript:
- Default import: import React from 'module';
- Named import: import { name } from 'module';
- Namespace import: import * as alias from 'module';
- Can combine default and named: import React, { useState } from 'module';
- Use imported identifiers directly in code.
Full Transcript
This visual execution shows how TypeScript handles different import styles. First, default imports bring in the main export without braces. Named imports use braces to pick specific exports. Namespace imports gather all exports under one alias. The execution table traces each import statement, showing the type, identifier, and usage. Variable tracker shows how identifiers start undefined and become linked to module exports after import. Key moments clarify common confusions like why default imports don't use braces and how namespace imports work. The quiz tests understanding of import types and syntax combinations. This helps beginners see how imports load and become usable in code.

Practice

(1/5)
1. Which import syntax correctly imports the default export from a module named utils?
easy
A. import utils from './utils';
B. import { utils } from './utils';
C. import * as utils from './utils';
D. import utils = require('./utils');

Solution

  1. Step 1: Understand default export import syntax

    The default export is imported without curly braces using import name from 'module'.
  2. 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.
  3. Final Answer:

    import utils from './utils'; -> Option A
  4. Quick Check:

    Default import = import name from 'module' [OK]
Hint: Default imports have no braces, named imports do [OK]
Common Mistakes:
  • Using curly braces for default imports
  • Confusing * as import with default import
  • Mixing CommonJS require syntax with ES modules
2. Which of the following is the correct syntax to import only the named export calculate from a module mathUtils?
easy
A. import calculate from './mathUtils';
B. import { calculate } from './mathUtils';
C. import * as calculate from './mathUtils';
D. import calculate = require('./mathUtils');

Solution

  1. Step 1: Identify named export import syntax

    Named exports are imported using curly braces: import { name } from 'module'.
  2. Step 2: Check each option

    import { calculate } from './mathUtils'; correctly imports the named export calculate. 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.
  3. Final Answer:

    import { calculate } from './mathUtils'; -> Option B
  4. Quick Check:

    Named import = import { name } from 'module' [OK]
Hint: Use curly braces for named imports only [OK]
Common Mistakes:
  • Omitting curly braces for named imports
  • Using * as import when only one named export is needed
  • Confusing default and named import syntax
3. Given the following code, what will be the output?
import * as helpers from './helpers';

console.log(typeof helpers);
console.log(typeof helpers.formatDate);

Assume helpers exports a function named formatDate.
medium
A. "object" and "object"
B. "function" and "undefined"
C. "object" and "function"
D. "undefined" and "function"

Solution

  1. Step 1: Understand import * as syntax

    Using import * as helpers imports all exports as an object named helpers.
  2. Step 2: Determine types of helpers and helpers.formatDate

    helpers is an object, so typeof helpers is "object". formatDate is a function exported inside helpers, so typeof helpers.formatDate is "function".
  3. Final Answer:

    "object" and "function" -> Option C
  4. Quick Check:

    import * as = object, function inside = function [OK]
Hint: import * as creates an object with exported members [OK]
Common Mistakes:
  • Thinking import * as imports a function directly
  • Confusing typeof results for functions and objects
  • Assuming default export with import * as syntax
4. Identify the error in this import statement:
import { default as myFunc } from './module';
medium
A. You cannot rename the default import using curly braces.
B. The syntax is correct; no error.
C. You must use import myFunc from './module'; for default imports.
D. Default imports must be imported with * as syntax.

Solution

  1. Step 1: Understand default import syntax

    Default imports are imported without curly braces and can be renamed directly: import myFunc from 'module'.
  2. Step 2: Analyze the given syntax

    The syntax import { default as myFunc } tries to rename the default export inside curly braces, which is invalid in ES module syntax.
  3. Final Answer:

    You cannot rename the default import using curly braces. -> Option A
  4. Quick Check:

    Default import no braces, no rename inside braces [OK]
Hint: Default imports never use curly braces [OK]
Common Mistakes:
  • Trying to rename default import inside curly braces
  • Confusing named export renaming with default import
  • Assuming default import can be destructured
5. You want to import a named export parseData from dataUtils but avoid naming conflicts by renaming it to parse. Which is the correct import statement?
hard
A. import parse from './dataUtils';
B. import { parse as parseData } from './dataUtils';
C. import * as parse from './dataUtils';
D. import { parseData as parse } from './dataUtils';

Solution

  1. Step 1: Understand renaming named imports

    You can rename named imports using import { originalName as newName }.
  2. Step 2: Check each option

    import { parseData as parse } from './dataUtils'; correctly renames parseData to parse. import parse from './dataUtils'; imports default export, not named. import * as parse from './dataUtils'; imports all exports as an object named parse. import { parse as parseData } from './dataUtils'; tries to rename parse to parseData, which is reversed.
  3. Final Answer:

    import { parseData as parse } from './dataUtils'; -> Option D
  4. Quick Check:

    Rename named import with 'as' inside braces [OK]
Hint: Use 'as' inside braces to rename named imports [OK]
Common Mistakes:
  • Using default import syntax for named exports
  • Confusing rename direction in 'as' syntax
  • Using * as when only one function is needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard