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.
import React from 'react'; import { useState } from 'react'; import * as Utils from './utils';
| Step | Import Statement | Import Type | Imported Identifier | Source Module | Usage Example |
|---|---|---|---|---|---|
| 1 | import React from 'react'; | Default | React | 'react' | React.createElement(...) |
| 2 | import { useState } from 'react'; | Named | useState | 'react' | const [state, setState] = useState() |
| 3 | import * as Utils from './utils'; | Namespace | Utils | './utils' | Utils.helperFunction() |
| 4 | End of imports | - | - | - | - |
| Identifier | Start | After Import |
|---|---|---|
| React | undefined | object (default export from 'react') |
| useState | undefined | function (named export from 'react') |
| Utils | undefined | object (all exports from './utils') |
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.utils?import name from 'module'.calculate from a module mathUtils?import { name } from 'module'.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.import * as helpers from './helpers'; console.log(typeof helpers); console.log(typeof helpers.formatDate);
helpers exports a function named formatDate.import * as syntaximport * as helpers imports all exports as an object named helpers.helpers and helpers.formatDatehelpers is an object, so typeof helpers is "object". formatDate is a function exported inside helpers, so typeof helpers.formatDate is "function".import { default as myFunc } from './module';import myFunc from 'module'.import { default as myFunc } tries to rename the default export inside curly braces, which is invalid in ES module syntax.parseData from dataUtils but avoid naming conflicts by renaming it to parse. Which is the correct import statement?import { originalName as newName }.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.Pass the test and this pattern moves unknown