Challenge - 5 Problems
Type-only Imports and Exports Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this TypeScript code with type-only import?
Consider the following TypeScript files. What will be the output when compiled and run with Node.js (ignoring type-only imports)?
Typescript
/* types.ts */ export type User = { name: string; age: number }; /* main.ts */ import type { User } from './types'; const user: User = { name: 'Alice', age: 30 }; console.log(user.name);
Attempts:
2 left
💡 Hint
Type-only imports are removed from the compiled JavaScript code.
✗ Incorrect
Type-only imports are used only during compilation for type checking and do not appear in the output JavaScript. The variable 'user' is defined with the type User, but the type is erased at runtime. The console.log prints 'Alice' as expected.
❓ Predict Output
intermediate2:00remaining
What error does this code raise due to incorrect type-only export?
Examine this TypeScript code snippet. What error will occur during compilation?
Typescript
/* data.ts */ const data = { id: 1, value: 'test' }; export type Data = typeof data; export default data; /* index.ts */ import data, { type Data } from './data'; const newData: Data = { id: 2, value: 'hello' }; console.log(newData);
Attempts:
2 left
💡 Hint
Type-only exports can be imported with 'type' keyword and used for typing only.
✗ Incorrect
The code correctly imports the default value 'data' and the type 'Data' separately. The type-only import is erased at runtime, so no error occurs. The console.log prints the newData object.
❓ Predict Output
advanced2:00remaining
What is the output of this code mixing type-only and value imports?
Analyze this TypeScript code. What will be printed to the console?
Typescript
/* shapes.ts */ export type Circle = { radius: number }; export const PI = 3.14; /* app.ts */ import type { Circle } from './shapes'; import { PI } from './shapes'; const circle: Circle = { radius: 5 }; console.log(PI * circle.radius * circle.radius);
Attempts:
2 left
💡 Hint
Type-only imports do not affect runtime, value imports do.
✗ Incorrect
The value PI is imported normally and used in the calculation. The type Circle is imported only for type checking and removed at runtime. The calculation outputs 78.5.
❓ Predict Output
advanced2:00remaining
What error occurs when using a type-only import as a value?
What happens when you try to use a type-only import as a value in this code?
Typescript
/* models.ts */ export type Product = { id: number; name: string }; /* index.ts */ import type { Product } from './models'; const p = Product; console.log(p);
Attempts:
2 left
💡 Hint
Type-only imports cannot be used as values at runtime.
✗ Incorrect
Since 'Product' is imported only as a type, it does not exist at runtime. Using it as a value causes a compilation error.
🧠 Conceptual
expert2:00remaining
Which statement about type-only imports and exports is true?
Select the correct statement about TypeScript's type-only imports and exports.
Attempts:
2 left
💡 Hint
Think about what happens to type-only imports during compilation.
✗ Incorrect
The 'import type' syntax tells TypeScript to import only the type information and remove it from the emitted JavaScript code, so it does not exist at runtime.