Primitive types in typescript

Written by Valdir Viana
Cover Image for Primitive types in typescript

Just like most programming languages used today, Typescript has its data types, but only some of them are considered primitive types. TypeScript suporta 7 tipos primitivos de dados sendo eles number, string, boolean, bigint, symbol, undefined e null.

The most common: string, number e boolean

These firs three are the most frequently used data types.

Type string

The string type is used to store textual data. A variable of type string can contain alphanumeric values and special characters.

When assigning a value to a string, use double quotes ("), single quotes (') or backticks (`).

let name: string = 'John Nobody from Nowhere';

let text: string = `A sentence written using multiple lines
             next line
             last line using Template literals`;

let street: string = "Street double quotes";

// Values like undefined and null are not accepted.
let naoPermitido1: string = undefined; // Type 'undefined' is not assignable to type 'string'.
let naoPermitido2: string = null; // Type 'null' is not assignable to type 'string'.

Type number

A variable of type number is used to store numeric data, originally in javascript there is no difference between int and float, just like in other languages. In javascript and also in typescript every numerical value is simply a number, which may contain an exact number or a number with a decimal.

let integerNumber: number = 10;

let decimalNumber: number = 10.5;

// Values like undefined and null are not accepted.
let notAllowed1: number = undefined; // Type 'undefined' is not assignable to type 'number'.
let notAllowed2: number = null; // Type 'null' is not assignable to type 'number'.

Type boolean

If it is necessary to store true and false values, the boolean type will be used.

let trueVar: boolean = true;

let falseVar: boolean = false;

// Values like undefined and null are not accepted.
let naoPermitido1: boolean = undefined; // Type 'undefined' is not assignable to type 'boolean'.
let naoPermitido2: boolean = null; // Type 'null' is not assignable to type 'boolean'.

Type bigint

The version ES2020 of ECMAScript, support for very large integers was introduced. In short, by using bigint it is possible to use numeric values beyond the limits of the number type. To create a bigint number, use the letter n at the end of the value.

const varBigInt: bigint = 100n;
const creationWithImplicitTyping = 100n;

typeof 1n === 'bigint' // true
typeof 1 === 'bigint'  // false    

Type symbol

There is a JavaScript primitive used to create a globally unique reference via the Symbol() function:

const sym1 = Symbol();
typeof sym; // "symbol"

const sym2 = Symbol("foo");
typeof sym2; // "symbol"

const sym3 = Symbol("foo");
typeof sym3; // "symbol"

Type null e undefined

In TypeScript as well as in Javascript there are two types of data that are used to represent the absence of value or the non-initialization of a given value.

  • undefined
let varTest;
console.log(varTest); // undefined
console.log(typeof varTest); // "undefined"

let varTest2: undefined = undefined;
console.log(typeof varTest2); // "undefined"

An undefined variable means that the variable has been declared but its value has not yet been defined.

  • null
let varTest;
let varNull: null = null;

console.log(varNull); // null
console.log(typeof varNull); // "object"

// Com declaração implicita
let varNullImplicit = null;

console.log(varNullImplicit); // null
console.log(typeof varNullImplicit); // "object"

A null variable represents an empty or nonexistent value. Another interesting feature, originally null is an object, which is a complex type in javascript.


These are the primitive types in Typescript, in addition to these primitive types there are also other data types that provide the language with a complete tooling.

References