Warning: session_start(): open(/tmp/sess_da0c3f8b6199dcbf747de8c24d9f5301, 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
How To Declare An Array Of Booleans In Typescript? - LearnShareIT

How To Declare An Array Of Booleans In Typescript?

Declare an Array of Booleans in TypeScript

You will usually work with an array in Typescript. Learning to declare an array of booleans in TypeScript is also essential. So how to do it? Let’s go into detail.

Declare an array of booleans in TypeScript

Like in JavaScript, you can declare an array of booleans in the usual way.

Example:

const anArray = [true, false];
console.log(anArray);

But the bad thing is by this declared, and your array will become any type so that you can add any type in your array like the example below:

Example:

const anArray = [true, false, "true"];
console.log(anArray);
[true, false, "true"]

Here I add a string that is “true” I can also make that mistake in real code, which then lead to an error. So Typescript provides a type to help us with this problem.

Declare an empty array with booleans type

In TypeScript, you can declare the type for your variable by adding type after the colon like this code below:

Example:

const anArray: boolean[] = [];
const anArray1: boolean[] = new Array();
const anArray2: boolean[] = Array();

const anArray3: Array<boolean> = [];
const anArray4: Array<boolean> = new Array();
const anArray5: Array<boolean> = Array();

Here you can declare type two-way by type[] or array <type>. It also means creating an array with the type that you have specified.

Use the “as” keyword

With the “as” keyword, you can specify the type you want to set to your variable.

Example:

const anArray1 = [] as boolean[];
const anArray2 = new Array() as boolean[];
const anArray3 = Array() as boolean[];

Here I create a new array with any type and then set the type to the boolean array by the “as” keyword.

Another way:

It also has a lot of ways to declare a booleans array too.

Example:

const anArray1 = new Array<boolean>();
const anArray2 = Array<boolean>();

But it is not our problem now.

After declaring a booleans array, you can add a new item by push method.

Example:

const anArray: boolean[] = [];
anArray.push(true); // Add one element
anArray.push(true, false, false); // Add multiple element
console.log(anArray);

Output:

[true, true, false, false]

Here if we pass in the wrong type will lead to an error.

Example:

const anArray: boolean[] = [];
anArray.push("aString");
console.log(anArray);

Error:

Argument of type 'string' is not assignable to parameter of type 'boolean'.

Summary

This tutorial shows you how to declare an array of booleans in Typescript. There are many ways, but you can choose the one which is suitable for you. If you have any problems, please comment below. Thanks for reading!

Maybe you are interested:

Leave a Reply

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