TS2678

error TS2678: Type ‘StreamStatus‘ is not comparable to type ‘number‘.

Broken Code ❌

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
enum StreamStatus {
ONLINE = 'ONLINE',
OFFLINE = 'OFFLINE'
}

interface StreamResponse {
status: number;
}

function handleResponse(response: StreamResponse): void {
switch (response.status) {
case StreamStatus.ONLINE:
console.log('You are online.');
break;
case StreamStatus.OFFLINE:
console.log('You are offline.');
break;
}
}

Fixed Code ✔️

The StreamResponse declares a “status” property of type number but the switch-case statement checks against StreamStatus, so we have to adjust the typing for “status” of StreamResponse:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
enum StreamStatus {
ONLINE = 'ONLINE',
OFFLINE = 'OFFLINE'
}

interface StreamResponse {
status: StreamStatus;
}

function handleResponse(response: StreamResponse): void {
switch (response.status) {
case StreamStatus.ONLINE:
console.log('You are online.');
break;
case StreamStatus.OFFLINE:
console.log('You are offline.');
break;
}
}