0
0
Typescriptprogramming~20 mins

Interface declaration syntax in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interface 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 TypeScript code using interface?

Consider this TypeScript code snippet. What will be logged to the console?

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

const user: Person = { name: "Alice", age: 30 };
console.log(user.name + " is " + user.age + " years old.");
ATypeError at runtime
B"Alice is 30 years old."
C"user.name is 30 years old."
D"Alice is age years old."
Attempts:
2 left
💡 Hint

Look at how the user object is created and how properties are accessed.

📝 Syntax
intermediate
2:00remaining
Which option correctly declares an interface with optional properties?

Which of the following interface declarations correctly defines an interface with an optional property email?

Ainterface User { name: string; email?: string; }
Binterface User { name: string; email: string?; }
Cinterface User { name: string; optional email: string; }
Dinterface User { name: string; email: optional string; }
Attempts:
2 left
💡 Hint

In TypeScript, optional properties use a question mark after the property name.

🔧 Debug
advanced
2:00remaining
What error does this interface declaration cause?

Examine the following TypeScript code. What error will the compiler report?

Typescript
interface Product {
  id: number;
  name: string;
  id: string;
}
AProperty 'id' must be a string.
BMissing semicolon after 'name' property.
CDuplicate identifier 'id'.
DNo error, code compiles fine.
Attempts:
2 left
💡 Hint

Check if property names can be repeated in an interface.

🧠 Conceptual
advanced
2:00remaining
What is the result of extending interfaces with conflicting property types?

Given these interfaces, what will happen if you declare a variable of type Combined?

interface A { value: string; }
interface B { value: number; }
interface Combined extends A, B {}
ATypeScript will report a conflict error for property 'value'.
BThe property 'value' will be of type 'string | number'.
CThe property 'value' will be of type 'any'.
DNo error; 'value' will be of type 'string'.
Attempts:
2 left
💡 Hint

Think about how TypeScript handles interface inheritance with conflicting property types.

🚀 Application
expert
2:00remaining
How many properties does this interface have after merging declarations?

Consider these two interface declarations in the same scope:

interface Config {
  host: string;
}
interface Config {
  port: number;
}

How many properties does the merged Config interface have?

A1
B3
C0
D2
Attempts:
2 left
💡 Hint

TypeScript allows interface merging. Properties from both declarations combine.