Recall & Review
beginner
What is an interface in TypeScript?
An interface in TypeScript defines a contract or a shape for objects. It specifies what properties and methods an object should have, without providing the implementation.
Click to reveal answer
beginner
How do you make a class follow an interface in TypeScript?You use the <code>implements</code> keyword after the class name, followed by the interface name. This means the class must provide all properties and methods defined in the interface.Click to reveal answer
intermediate
What happens if a class does not implement all properties or methods of an interface it claims to implement?TypeScript will show an error because the class does not fulfill the contract of the interface. The class must implement all required members.Click to reveal answer
intermediate
Can a class implement multiple interfaces in TypeScript?Yes, a class can implement multiple interfaces by listing them separated by commas after the <code>implements</code> keyword.Click to reveal answer
beginner
Example: What does this code do?<br><pre>interface Animal {<br> name: string;<br> makeSound(): void;<br>}<br><br>class Dog implements Animal {<br> name: string;<br> constructor(name: string) {<br> this.name = name;<br> }<br> makeSound() {<br> console.log('Woof!');<br> }<br>}</pre>This code defines an interface
Animal with a property name and a method makeSound. The class Dog implements this interface by providing the name property and the makeSound method that prints 'Woof!'.Click to reveal answer
Which keyword is used in TypeScript for a class to follow an interface?
✗ Incorrect
The
implements keyword is used for a class to follow an interface contract.What will happen if a class misses a method defined in its interface?
✗ Incorrect
TypeScript enforces that all interface members must be implemented, otherwise it shows an error.
Can a class implement more than one interface?
✗ Incorrect
A class can implement multiple interfaces by listing them separated by commas.
What does an interface define?
✗ Incorrect
An interface defines the shape or contract an object must follow, including properties and method signatures.
In this code snippet, what is the role of
makeSound()?<br>interface Animal {<br> makeSound(): void;<br>}<br>class Cat implements Animal {<br> makeSound() { console.log('Meow'); }<br>}✗ Incorrect
makeSound() is a method signature in the interface that the class must implement.Explain how a class implements an interface in TypeScript and why it is useful.
Think about how interfaces act like a promise for the class.
You got /4 concepts.
Describe what happens if a class does not fully implement an interface it claims to implement.
What does TypeScript do to keep your code correct?
You got /4 concepts.