0
0
Typescriptprogramming~20 mins

Import syntax variations in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Import Syntax 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 import usage?
Given the following TypeScript module and import statements, what will be the output when running console.log?
Typescript
/* module.ts */
export const value = 42;
export default function greet() { return 'Hello'; }

/* main.ts */
import greet, { value } from './module';
console.log(greet(), value);
ASyntaxError
B"undefined 42"
C"Hello undefined"
D"Hello 42"
Attempts:
2 left
💡 Hint
Default exports can be imported with any name, named exports must match exactly.
Predict Output
intermediate
2:00remaining
What error occurs with this import statement?
Consider this import statement in TypeScript:
import { default as greet } from './module';
where module.ts has a default export function. What happens when you run this?
Typescript
/* module.ts */
export default function greet() { return 'Hi'; }

/* main.ts */
import { default as greet } from './module';
console.log(greet());
ASyntaxError: Unexpected token 'default'
BRuns and prints 'Hi'
CTypeError: greet is not a function
DModule not found error
Attempts:
2 left
💡 Hint
Default exports cannot be imported inside curly braces as 'default'.
Predict Output
advanced
2:00remaining
What is the output of this namespace import?
Given module.ts exports multiple named constants, what does this code print?
import * as mod from './module';
console.log(mod.a, mod.b);
Typescript
/* module.ts */
export const a = 1;
export const b = 2;

/* main.ts */
import * as mod from './module';
console.log(mod.a, mod.b);
ASyntaxError
Bundefined undefined
C1 2
DTypeError: mod.a is not a function
Attempts:
2 left
💡 Hint
Namespace import collects all named exports as properties.
Predict Output
advanced
2:00remaining
What is the output when importing a type only?
Given this code, what will be printed?
import type { User } from './types';
const user: User = { name: 'Alice' };
console.log(typeof user);
Typescript
/* types.ts */
export interface User { name: string; }

/* main.ts */
import type { User } from './types';
const user: User = { name: 'Alice' };
console.log(typeof user);
A"object"
BTypeError
C"undefined"
D"User"
Attempts:
2 left
💡 Hint
Type imports are erased at runtime, so user is a normal object.
🧠 Conceptual
expert
2:00remaining
Which import syntax correctly imports a default export and renames it?
You have a module with a default export function. Which import statement correctly imports it and renames it to myFunc?
Aimport { default as myFunc } from './module';
Bimport myFunc from './module';
Cimport * as myFunc from './module';
Dimport { myFunc } from './module';
Attempts:
2 left
💡 Hint
Default exports are imported without curly braces and can be renamed by the import name.