Typescript for beginners: boolean

evaldasburlingis

Evaldas

Posted on September 14, 2020

Typescript for beginners: boolean

Boolean is the data type that has two possible values. It is either true or false.

To do type checking on the variable we can write a statement as follows

const isTrue : boolean = true;

Step-by-step:

  • Define variable const isTrue
  • Add typescript type check : boolean
  • Define value true

To check whether function is returning boolean we can do it as fallows:

function isOlder(age : number) : boolean {
  return age >= 18 ? true : false;
}

isOlder(18) // true
isOlder(16) // false
isOlder(true) // error 'Argument of type boolean is not assignable to parameter of type number'

Step-by-step:

  • Define function function isOlder(argument : typecheck) : type to return {...}
💖 💪 🙅 🚩
evaldasburlingis
Evaldas

Posted on September 14, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related