0
0
Typescriptprogramming~20 mins

Why interfaces are needed in Typescript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use interfaces in TypeScript?

Which of the following best explains why interfaces are needed in TypeScript?

AInterfaces replace classes and remove the need for functions.
BInterfaces allow you to write code that runs faster by optimizing JavaScript execution.
CInterfaces automatically generate user interfaces for web applications.
DInterfaces define a contract for objects, ensuring consistent structure and enabling type checking.
Attempts:
2 left
💡 Hint

Think about how interfaces help with organizing and checking data shapes.

Predict Output
intermediate
2:00remaining
Output of interface usage in TypeScript

What will be the output of the following TypeScript code when compiled and run?

Typescript
interface Person {
  name: string;
  age: number;
}

function greet(person: Person) {
  return `Hello, ${person.name}. You are ${person.age} years old.`;
}

const user = { name: "Alice", age: 30 };
console.log(greet(user));
AHello, Alice. You are 30 years old.
BTypeError at runtime because user is missing properties.
CCompilation error: user does not implement Person interface.
DHello, undefined. You are undefined years old.
Attempts:
2 left
💡 Hint

Interfaces are only for compile-time checks and do not affect runtime objects.

🔧 Debug
advanced
2:00remaining
Identify the error with interface implementation

What error will this TypeScript code produce?

Typescript
interface Vehicle {
  wheels: number;
  drive(): void;
}

const bike: Vehicle = {
  wheels: 2
};
ANo error, code runs fine.
BError: Property 'drive' is missing in type '{ wheels: number; }' but required in type 'Vehicle'.
CError: 'wheels' property must be a string, not a number.
DRuntime error: drive is not a function.
Attempts:
2 left
💡 Hint

Check if all required properties and methods of the interface are present.

📝 Syntax
advanced
2:00remaining
Correct interface syntax in TypeScript

Which option shows the correct syntax to declare an interface with a required string property 'title' and an optional number property 'year'?

Ainterface Book { title: string; year?: 'number'; }
Binterface Book { title: string; optional year: number; }
Cinterface Book { title: string; year?: number; }
Dinterface Book { title: string; year: number | undefined; }
Attempts:
2 left
💡 Hint

Remember how optional properties are marked in TypeScript interfaces.

🚀 Application
expert
3:00remaining
Using interfaces to enforce consistent function parameters

You want to create a function that accepts an object with properties 'x' and 'y' as numbers. Which interface and function definition ensure this and allow TypeScript to check the input?

A
interface Point { x: number; y: number; }
function printPoint(p: Point) { console.log(`X: ${p.x}, Y: ${p.y}`); }
B
interface Point { x: number; y: number; }
function printPoint(p: { x: string; y: string }) { console.log(`X: ${p.x}, Y: ${p.y}`); }
C
interface Point { x: number; y: number; }
function printPoint(p: any) { console.log(`X: ${p.x}, Y: ${p.y}`); }
D
interface Point { x: number; y: number; }
function printPoint(p: { x: number }) { console.log(`X: ${p.x}, Y: ${p.y}`); }
Attempts:
2 left
💡 Hint

Check that the function parameter type matches the interface exactly.