An interface in TypeScript defines a structure for objects, specifying what properties and methods they should have without implementing them.
Click to reveal answer
beginner
How do you declare a simple interface with two properties: name (string) and age (number)?
Use the interface keyword followed by the interface name and property types:
interface Person {
name: string;
age: number;
}
Click to reveal answer
intermediate
Can interfaces in TypeScript include methods? How do you declare one?
Yes, interfaces can include method signatures without implementation. For example:
interface Person {
greet(): void;
}
This means any object implementing Person must have a greet method that returns nothing.
Click to reveal answer
intermediate
What does it mean when a property in an interface is marked optional with a question mark (?)?
An optional property means objects implementing the interface may or may not have that property. For example:
interface Person {
middleName?: string;
}
The middleName property can be missing.
Click to reveal answer
intermediate
How do you extend an interface from another interface in TypeScript?
Use the extends keyword to create a new interface that includes all properties of the original plus new ones:
interface Employee extends Person {
employeeId: number;
}
Click to reveal answer
Which keyword is used to declare an interface in TypeScript?
Aclass
Binterface
Ctype
Dstruct
✗ Incorrect
The interface keyword is used to declare interfaces in TypeScript.
How do you mark a property as optional in an interface?
AUse the keyword optional before the property
BSet the property type to undefined
CAdd a question mark (?) after the property name
DUse the keyword nullable
✗ Incorrect
A question mark (?) after the property name marks it as optional.
What does this interface declaration mean?
interface Car { drive(): void; }
Adrive is a method that must return a string
Bdrive is a property of type void
Cdrive is a variable to store a function
DAny object with a drive method that returns nothing matches this interface
✗ Incorrect
The interface requires a drive method that returns nothing (void).
How do you create an interface that includes all properties of another interface plus new ones?
AUse the extends keyword
BUse the implements keyword
CUse the include keyword
DUse the inherit keyword
✗ Incorrect
The extends keyword is used to inherit properties from another interface.
Which of these is a valid interface declaration?
Ainterface User { id: number; name: string; }
Binterface User = { id: number; name: string; }
Cinterface User (id: number, name: string)
Dinterface User { id => number; name => string; }
✗ Incorrect
Option A uses correct TypeScript interface syntax.
Explain how to declare an interface with optional properties and methods in TypeScript.
Think about how to mark properties optional and how methods are declared without code.
You got /4 concepts.
Describe how interface inheritance works in TypeScript and why it is useful.
Consider how one interface can build on another.
You got /4 concepts.
Practice
(1/5)
1. What is the purpose of an interface in TypeScript?
easy
A. To import external modules
B. To execute code conditionally
C. To define the shape of an object with properties and methods
D. To create a new variable
Solution
Step 1: Understand the role of interfaces
Interfaces describe how an object should look by specifying its properties and methods.
Step 2: Compare with other options
Options A, B, and C describe unrelated concepts like control flow, variables, and modules.
Final Answer:
To define the shape of an object with properties and methods -> Option C
Quick Check:
Interface = Object shape definition [OK]
Hint: Interfaces define object structure, not code execution [OK]
Common Mistakes:
Confusing interfaces with classes
Thinking interfaces run code
Mixing interfaces with variables
2. Which of the following is the correct syntax to declare an interface named Person with a string property name?
easy
A. interface Person { name = string; }
B. interface Person = { name: string; }
C. interface Person (name: string);
D. interface Person { name: string; }
Solution
Step 1: Recall interface declaration syntax
Interfaces use the keyword interface, followed by the name and curly braces with property declarations using colon syntax.
Step 2: Check each option
interface Person { name: string; } uses correct syntax: interface Person { name: string; }. Options B, C, and D have syntax errors like using equals, parentheses, or assignment instead of colon.
Final Answer:
interface Person { name: string; } -> Option D
Quick Check:
Interface syntax uses colon for properties [OK]
Hint: Use colon, not equals, to declare properties in interfaces [OK]
Common Mistakes:
Using '=' instead of ':' for properties
Using parentheses instead of braces
Assigning values inside interface
3. What will be the output of the following TypeScript code?
Step 1: Understand optional properties in interfaces
The description property is optional (marked with ?), so it can be omitted.
Step 2: Check each object against the interface
{ id: 101, name: "Book" } has required id (number) and name (string), no description, which is valid. { id: 101, description: "A book" } misses required name. { name: "Book", description: "A book" } misses required id. { id: "101", name: "Book" } has id as string instead of number.
Final Answer:
{ id: 101, name: "Book" } -> Option A
Quick Check:
Optional properties can be omitted safely [OK]
Hint: Optional properties can be left out without error [OK]
Common Mistakes:
Omitting required properties
Using wrong types for properties
Confusing optional with required
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.