Objects 16 exercises
Problem

An Issue with Duplicate Interfaces

In this exercise, we'll look at an interesting issue related to interfaces in TypeScript.

Here we have an object, myLogger, that is typed as Logger. However, there are two Logger interfaces defined:


interface Logger {
log(message: string, level: number): void;
}
interface

Loading exercise

Transcript

00:00 In this exercise, we're going to look at a curious property of interfaces. We have myLogger here, which is an object which is being assigned the type of logger, which is declared as an interface or in fact as two interfaces above it. And really we want our log function to only need message string inside here.

00:19 And yet we're getting an error here saying that this level thing shouldn't be required, except currently it is required. So if we hover over logger.log, it's requiring message string and level number, even though this log inside myLogger is not. It only needs message string.

00:37 Your job here is to try to work out why this bug is occurring and what we can do to fix it. And notice that there are a couple of loggers here with the same name. Interesting. Good luck.