0
0
Typescriptprogramming~5 mins

Type-only imports and exports in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a type-only import in TypeScript?
A type-only import brings in types from another file without including any JavaScript code in the final output. It uses the <code>import type</code> syntax.
Click to reveal answer
beginner
How do you write a type-only export in TypeScript?
You use export type before the type or interface you want to export, so it only exports the type information without any runtime code.
Click to reveal answer
intermediate
Why use type-only imports and exports?
They help keep the JavaScript output clean by removing unnecessary code, improving performance and avoiding circular dependencies at runtime.
Click to reveal answer
intermediate
What happens if you import a type normally instead of using <code>import type</code>?
TypeScript may include extra JavaScript code in the output, which can increase bundle size and cause runtime errors if the imported item is not a value.
Click to reveal answer
beginner
Show an example of a type-only import and a type-only export.
Example:<br><pre>import type { User } from './models';

export type ID = string | number;</pre>
Click to reveal answer
Which syntax imports only types without including JavaScript code?
Aimport { MyType } from './file';
Bimport type { MyType } from './file';
Cexport type { MyType } from './file';
Dexport { MyType } from './file';
What keyword is used to export only types in TypeScript?
Aexport type
Bexport interface
Cexport const
Dexport class
Why might you want to use type-only imports?
ATo reduce JavaScript bundle size
BTo import runtime values
CTo execute code immediately
DTo create side effects
What happens if you import a type without import type and the type is not a value?
AThe program crashes immediately
BThe import is ignored
CExtra JavaScript code may be included
DThe type is converted to a value
Which of these is a correct type-only export?
Aexport interface User {}
Bexport const User = { name: 'Alice' };
Cexport class User {}
Dexport type User = { name: string };
Explain what type-only imports and exports are and why they are useful.
Think about how TypeScript separates types from JavaScript code.
You got /3 concepts.
    Write a small example showing how to import a type only and export a type only in TypeScript.
    Use <code>import type</code> and <code>export type</code> keywords.
    You got /3 concepts.