0
0
Typescriptprogramming~20 mins

Type-only imports and exports in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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
intermediate
2: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);
AAlice
Bundefined
CCompilation error due to type-only import
DRuntime error: User is not defined
Attempts:
2 left
💡 Hint
Type-only imports are removed from the compiled JavaScript code.
Predict Output
intermediate
2: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);
AError: Default import 'data' is not a constructor
BRuntime error: newData is undefined
CError: Cannot use type-only export 'Data' as a value
DNo error, outputs { id: 2, value: 'hello' }
Attempts:
2 left
💡 Hint
Type-only exports can be imported with 'type' keyword and used for typing only.
Predict Output
advanced
2: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);
ACompilation error: Cannot mix type-only and value imports
BNaN
C78.5
DRuntime error: PI is undefined
Attempts:
2 left
💡 Hint
Type-only imports do not affect runtime, value imports do.
Predict Output
advanced
2: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);
ACompilation error: 'Product' only refers to a type but is used as a value here
BRuntime error: Product is undefined
COutputs the type definition of Product
DNo error, outputs the Product object
Attempts:
2 left
💡 Hint
Type-only imports cannot be used as values at runtime.
🧠 Conceptual
expert
2:00remaining
Which statement about type-only imports and exports is true?
Select the correct statement about TypeScript's type-only imports and exports.
AType-only imports allow importing runtime values without type information.
BUsing 'import type' ensures the imported entity is removed from the JavaScript output.
CType-only imports are included in the compiled JavaScript output as variables.
DType-only exports can be imported without the 'type' keyword and used as runtime values.
Attempts:
2 left
💡 Hint
Think about what happens to type-only imports during compilation.