0
0
Typescriptprogramming~5 mins

Import syntax variations in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the syntax to import the default export from a module in TypeScript?
Use <code>import name from 'module'</code> where <code>name</code> is the local name you want to assign to the default export.
Click to reveal answer
beginner
How do you import named exports from a module?
Use <code>import { export1, export2 } from 'module'</code> to import specific named exports.
Click to reveal answer
intermediate
Explain how to import all exports from a module as a single object.
Use <code>import * as alias from 'module'</code>. This imports everything and groups it under the name <code>alias</code>.
Click to reveal answer
intermediate
What is the syntax to rename a named import during import?
Use <code>import { originalName as aliasName } from 'module'</code> to rename a named export locally.
Click to reveal answer
intermediate
How can you import a module for its side effects only?
Use <code>import 'module'</code> without any bindings. This runs the module code but does not import any values.
Click to reveal answer
Which syntax imports the default export from a module named 'utils'?
Aimport * as utils from 'utils'
Bimport { utils } from 'utils'
Cimport utils from 'utils'
Dimport 'utils'
How do you import only the named export 'calculate' from a module 'math'?
Aimport { calculate } from 'math'
Bimport 'math'
Cimport * as calculate from 'math'
Dimport calculate from 'math'
What does import * as utils from 'utils' do?
AImports all exports grouped under the name 'utils'
BImports only the default export
CImports nothing, just runs the module
DImports only named exports without default
How do you rename a named import 'foo' to 'bar'?
Aimport { bar as foo } from 'module'
Bimport { foo as bar } from 'module'
Cimport foo from 'module' as bar
Dimport * as bar from 'module'
Which import syntax runs a module only for its side effects?
Aimport * as mod from 'module'
Bimport mod from 'module'
Cimport { something } from 'module'
Dimport 'module'
Describe the different ways to import modules in TypeScript and when to use each.
Think about how you get one export, many exports, or just run the module.
You got /5 concepts.
    Explain how to rename a named export during import and why it might be useful.
    Use the 'as' keyword in import statements.
    You got /3 concepts.