Typescript Datatypes - tutorials with examples
- Admin
- Mar 6, 2024
- Typescript
Once you read Typescript Installation and Hello world program article, You will get a clear understanding of how to install typescript and write the hello world program.
In this article, I will walk you through the basic data types of the TypeScript language.
TypeScript Data Types
TypeScript encompasses all data types supported by JavaScript.
The supported data types in TypeScript include void
, Null
, undefined
, boolean
, string
, Number
, array
, tuple
data types, Enum
, Any
and Never
data types.
Syntax:
Variables can be declared using the following syntax:
let/var/const variableName: datatype;
Variable
names are valid, and data types are either built-in types or custom data types as objects. let
, var
, and const
are keywords used to declare variables based on the variable scope.
How to Declare Boolean Data Type in TypeScript?
Boolean is a basic data type that contains true or false values. It’s also available in JavaScript as well as other programming languages like Java.
Boolean types are used in if-else and while logical flows.
let isChecked: boolean; // valid
let isFlag: boolean = true; // valid
let isValid1: boolean = 213; // Not valid, gives a compile-time error
let isValid2: boolean = "test"; // Not valid, gives a compile-time error
isValid1
and isValid2
are not valid values and give the error Type ‘213’ is not assignable to type ‘boolean’ compile-time error.
Declare Number
TypeScript does not have an integer or long data type, but it does have a number
data type that defaults to a floating number.
We can represent integer
and floating
values in the number data type.
let n1: number = 123; // valid
let n2: number = 0o987; // Octal
let n3: number = 0xbac01; // Hexadecimal
let n4: number = "string"; // Not valid, gives an error
let n5: number = null; // Valid
let n6: number = undefined; // Valid
String Data Type
The string
is a common data type in any programming language. It represents a group of characters stored under a variable of String type.
The string can be declared using double
quotes or single
quotes. Please see String Complete examples
let s1: string = undefined; // valid
let s2: string = null; // valid
let s3: string = ""; // valid
let s4: string = "building"; // valid
let s5: string = 12; // Not valid
How to Declare Array Type Object in TypeScript
Arrays
are important data types available in JavaScript and TypeScript.
Arrays group multiple values with a single name, and the size of an array is not known. Arrays are one of the data types in TypeScript collection. It can be created in multiple ways like Generic Arrays or typed Arrays.
let numberArrays: number[] = [11, 2, 10]; // Number Generic Arrays
let stringArrays: string[] = ["jan", "Feb", "Mar", "Apr"]; // valid array
let numberArrays: number[] = [1, "Jan", "Feb"]; // Not valid array
let months: Array = ["January", "February", "March"]; // Typed array declaration
Tuple Data Type
Tuple
data type is used to create a fixed group of different data type values. Data types can be different.
let dataMix: [number, string];
dataMix = [2000, "Jan salary"];
Enum Data Type
Enum
is to represent the collection values stored under one name called Enumeration of items. It is the Enumeration of items.
enum Diagram {Rectangle, Circle, Square};
let myFirstDiagram: Diagram = Diagram.Rectangle;
please see Detailed Enum tutorials with all examples🔗
Void Data Type
void
means nothing. The void is used in the return type of methods/functions. Void
in the method means it doesn’t return anything.
function testVoidMethod(): void {
console.log("this is to test Void data type");
}
Undefined Data Type
In JavaScript, if any variable is not initialized with any value, by default, it initializes with undefined
. We can also assign an undefined
value.
let numberUndefined: number = undefined; // valid
let voidUndefined: void = undefined; // Valid
Any and Never Data Type
any
data type is used to hold the values of different data types.
let stringValue: any = "anyvalue";
let booleanValue: any = true;
let arrayValues: any[] = ["anyvalue", true, null, 986];
let numberValue: any = 54;
Never
data type is used to specify the data/function flow will never run. It uses function return types as well as variables.
function infiniteMethod(): never {
while (true) {}
return "this line never runes ";
}
Please check other articles on typescript tutorial series
You can check Typescript Related Posts: