Warning: session_start(): open(/tmp/sess_17277bc4cfb78650f49df7ef309aaabc, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
Type '#' is not assignable to type 'boolean' in TypeScript - How to fix? - LearnShareIT

Type ‘#’ is not assignable to type ‘boolean’ in TypeScript – How to fix?

Type '#' is not assignable to type 'boolean' in TypeScript

If you are having trouble with the error message “Type ‘#’ is not assignable to type ‘boolean'” in TypeScript, this article is for you. We will show you how to cast a variable and use the type guard to solve this problem. Check out the information below for detailed instructions.

Why does the error “Type ‘#’ is not assignable to type ‘boolean'” occur in your project?

This error occurs when you assign a value of the type that is not ‘boolean’ to a variable that expects a value of type ‘boolean’.

Let’s take a look at this example for a better understanding:

const age = 22;
const boolValue: boolean = age;

Error:

Type 'number' is not assignable to type 'boolean'.

As you can see, variable ‘age’ has type ‘number’ while variable ‘boolValue’ is expected to be of type ‘boolean’. When you perform the assignment, it will cause a conflict between the two data types. 

As a result, we get the error message as shown in the output.

How to solve the error “Type ‘#’ is not assignable to type ‘boolean'” in TypeScript?

The two methods we cover below will help you avoid getting the error “Type ‘#’ is not assignable to type ‘boolean'” in TypeScript.

Converting the value

You can use the Boolean() function to convert the variable’s type to ‘boolean’. Any value we pass as a parameter to the Boolean() function is returned to ‘true’ or ‘false’. Here is an example:

// Using Boolean() function
const age = Boolean(22);
const boolValue: boolean = age;
console.log(boolValue);

Output:

true

In this example, we pass in the number 22, so you can see we received ‘true’ in the output. If you pass in ‘0’, the result will be ‘false’.

Using type guard

We use the ternary operator to check the type of the variable. If the value is of type ‘boolean’, we will assign that value to the variable. Otherwise, assign the variable the value ‘false’.

const age = 22;

// Using ternary operator to verify the variable type
const boolValue: boolean = typeof age === "boolean" ? age : false;
console.log(boolValue);

Output:

false

In a particular case, if you already know the exact type of the value to be ‘boolean’, then you can use type assertions to fix this problem. For example:

const myBoolValue: unknown = false;

// Using type assertion
const newBoolValue: boolean = myBoolValue as boolean;

console.log(newBoolValue);

Output:

false

Summary

Finally, we just learned about the error “Type ‘#’ is not assignable to type ‘boolean'” in TypeScript. To solve this error, you should use the type guard to verify the variable type or convert the value to boolean type before the assignment. That’s the end of this article. Hopefully, the information we provide is helpful to you.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *