Benjamin Semah
Benjamin Semah

Follow

Benjamin Semah

Follow

Learn Primitive Types in TypeScript - Boolean, Number, String.

Benjamin Semah's photo
Benjamin Semah
·Sep 26, 2022·

3 min read

Learn Primitive Types in TypeScript - Boolean, Number, String.
Play this article

Table of contents

  • What are data types?
  • String Types
  • Number Types
  • Boolean Types
  • Conclusion

What are data types?

In programming, values are classified into types. The type of a value regulates its behavior. It also determines what operations can be used on it without causing an error.

Don't worry if this is a lot to take in at the moment. We'll look at examples to understand what it means.

Examples of data types in JavaScript include String, Number, Boolean, Object, and others.

Like JavaScript, TypeScript also has data types. In this post, you will learn about the most common types you might encounter when working with TypeScript. These basic types are what form the core building blocks of more complex types.

String Types

String Types are values that represent texts in a program. To declare a string type in JavaScript, you enclose the value in single quotes (' '), double quotes (" "), or backticks (`` ).

Examples of strings in JavaScript

// JavaScript
const myGreeting = "Hello"

let lastName = 'Jordan'

let fullName = `Michael ${lastName}`
console.log(fullName) // Prints Michael Jordan

fullName = 23
console.log(fullName) // Prints 23

Note: JavaScript allowed changing the type of the fullName variable from String to Number. This is a result of the Dynamic Type Nature of JavaScript. Now let's see how String Types are declared in TypeScript and how it defers from JavaScript.

// TypeScript
const myGreeting: string = "Hello"

let lastName: string = 'Jordan'

let fullName: string = `Michael ${lastName}`
console.log(fullName) // Prints Michael Jordan

fullName = 23 // Type 'number' is not assignable to type 'string'.

The syntax for assigning type in Typescript is a colon : followed by the name of the type, which is string in this case.

Note how TypeScript throws the error Type 'number' is not assignable to type 'string'. when we attempted to assign a number to the fullName variable. This is due to the Static Type nature of TypeScript.

Number Types

Number Types are precisely what the name says. They are number values in a program. Defining number types in TypeScript uses the same syntax as we used for the string types.

// TypeScript
const myVariable: number = 3

let playerNumber: number = 23
console.log(playerNumber) // Prints 23

playerNumber = "MJ" // Type 'string' is not assignable to type 'number'

Again, note what happens when we attempt to assign "MJ" which is a string to the playerNumber variable which has already been declared as a number type. TypeScript throws the following error Type 'string' is not assignable to type 'number'.

Boolean Types

Boolean can be one of two values. Either true or false. Let's see an example of assigning a boolean type to a variable in TypeScript. The syntax is the same as what we used for both string types and number types.

// TypeScript
const isLoggedIn: boolean = false

let isFriendly: boolean = true
console.log(isFriendly) // Prints true

isFriendly = "yes" //

Again, TypeScript prompts us with an error when we attempt to assign a string type "yes" to the isFriendly variable. The error Type 'string' is not assignable to type 'boolean'. is reminding us that type is static and cannot be changed. Therefore, isFriendly which has a type of boolean cannot be changed to a string type.

Conclusion

In this article, we have discussed the most basic types in TypeScript. These are;

  • String Types.
  • Number Types.
  • Boolean Types.

You learned how they are declared. You also looked at how they differ from their corresponding types in JavaScript.


Thanks for reading.

Let's connect on LinkedIn | Twitter

 
Share this