Type 'ConnectionState' is not assignable to type 'boolean'

I am getting this error when I use a function to monitor whether the internet is there or not. Can anyone help me to resolve this error? Here is the app,component.ts file
import { Component } from '@angular/core';
import { ConnectionService, ConnectionState } from 'ng-connection-service';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
title = 'finalProject';
status = 'ONLINE'; //initializing as online by default
isConnected: ConnectionState = false;

constructor(private connectionService: ConnectionService) {
this.connectionService.monitor().subscribe((isConnected) => {
this.isConnected = isConnected;
if (this.isConnected) {
this.status = 'ONLINE';
} else {
this.status = 'OFFLINE';
}
alert(this.status);
});
}
}
import { Component } from '@angular/core';
import { ConnectionService, ConnectionState } from 'ng-connection-service';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
title = 'finalProject';
status = 'ONLINE'; //initializing as online by default
isConnected: ConnectionState = false;

constructor(private connectionService: ConnectionService) {
this.connectionService.monitor().subscribe((isConnected) => {
this.isConnected = isConnected;
if (this.isConnected) {
this.status = 'ONLINE';
} else {
this.status = 'OFFLINE';
}
alert(this.status);
});
}
}
5 Replies
Jochem
Jochem16mo ago
you're defining isConnected as having type ConnectionState, and it's telling you a variable of type ConnectionState can't have a value of false
Adarsh
Adarsh16mo ago
import { Component } from '@angular/core';
import { ConnectionService, ConnectionState } from 'ng-connection-service';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
title = 'finalProject';
status = 'ONLINE'; //initializing as online by default
isConnected: boolean = true;

constructor(private connectionService: ConnectionService) {
this.connectionService.monitor().subscribe((isConnected) => {
this.isConnected = isConnected;
if (this.isConnected) {
this.status = 'ONLINE';
} else {
this.status = 'OFFLINE';
}
alert(this.status);
});
}
}
import { Component } from '@angular/core';
import { ConnectionService, ConnectionState } from 'ng-connection-service';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
title = 'finalProject';
status = 'ONLINE'; //initializing as online by default
isConnected: boolean = true;

constructor(private connectionService: ConnectionService) {
this.connectionService.monitor().subscribe((isConnected) => {
this.isConnected = isConnected;
if (this.isConnected) {
this.status = 'ONLINE';
} else {
this.status = 'OFFLINE';
}
alert(this.status);
});
}
}
I've made it like this still I'm getting the same error
Jochem
Jochem16mo ago
ConnectionState has this signature:
export interface ConnectionState {
/**
* "True" if browser has network connection. Determined by Window objects "online" / "offline" events.
*/
hasNetworkConnection: boolean;
/**
* "True" if browser has Internet access. Determined by heartbeat system which periodically makes request to heartbeat Url.
*/
hasInternetAccess: boolean;
}
export interface ConnectionState {
/**
* "True" if browser has network connection. Determined by Window objects "online" / "offline" events.
*/
hasNetworkConnection: boolean;
/**
* "True" if browser has Internet access. Determined by heartbeat system which periodically makes request to heartbeat Url.
*/
hasInternetAccess: boolean;
}
I'm guessing the connectionService subscription is passing in a ConnectionState object, and not just a boolean so when you're assigning isConnected to this.isConnected, it tells you you can't assign a ConnectionState variable to a boolean one probably this will solve your issue, instead of this.isConnected = isConnected; use this
this.isConnected = isConnected.hasInternetAccess;
this.isConnected = isConnected.hasInternetAccess;
Adarsh
Adarsh16mo ago
Yeah it did, thanks so much @jochemm
Jochem
Jochem16mo ago
no problem!