Article Sponsored by NammaHosting. Get upto 20% offer on purchases use code “BLEEDBYTES”.
TypeScript is a super-set of JavaScript, adds strict syntactical type checking and static typing. TypeScript must be trans-compiled into JavaScript for production. TypeScript compiles to plain JavaScript supported in ECMAScript3 and above versions which is supported by all browsers and nodejs run time engines.
Why TypeScript
- TypeScript simplifies JavaScript code, making it easier to read and debug.
- TypeScript is opensource.
- TypeScript provides highly productive development tools for JavaScript IDEs and practices, like static checking.
- TypeScript makes code easier to read and understand.
- With TypeScript, we can make a huge improvement over plain JavaScript.
- TypeScript gives us all the benefits of ES6 (ECMAScript 6), plus more productivity.
- TypeScript can help us to avoid painful bugs that developers commonly run into when writing JavaScript by type – checking the code.
- Powerful type system, including generics.
- TypeScript is nothing but JavaScript with some additional features.
- TypeScript code can be compiled as per ES5 and ES6 standards to support the latest browser.
- Supports static typing.
- TypeScript will save developers time.
In this article I am going to list out the data types with their example snippetsData Types in TypeSript are boolean
, number
, string
, Array
, Object
, undefined
, null
, enum
, void
, any
, tuple
and never
.
Boolean
To use boolean in TypeScript use boolean keyword while declaring a variable.
let isAvailable: boolean = true;
Number
In TypeScript numbers are floating point values. The values which access number
datatype are decimal, binary, hexadecimal and octal values.
let decimal: number = 15;
let hex: number = 0xf00d;
let binary: number = 0b1010;
let octal: number = 0o744;
String
Alike JavaScript TypeScript allows us to use string data type with single ('
) and double ("
) quotes. TypeScript also supports Template literals.
let sky: string = 'blue';
sky = 'orange';
let firstname: string = 'Santhosh';
let age: number = 22;
// Template Literals
let aboutme: string = `My name is ${firstname} and I am ${age} years old`;
Array
Array declaration in typescript is available in two ways, first one uses array bracket suffixed with datatype number[]
and second one generic array type Array<number>
.
let fruits: string[] = ["Apple","Orange","Grapes"];
let languages: Array<string> = ["English","Tamil","Hindi"];
Tuple
Tuple is an array with a fixed number of elements whose types are known, but need not be the same.
let student: [string, number] = ['Santhosh', 22];
Enum
Enum datatypes with more friendly names to numeric values. Enums are added feature in TypeScript which isn’t available in JavaScript.With enum we can access values with keys and vice versa.
enum Color { yellow, green, red = 6 }
let color: Color = Color.green; // access value with key
let colorName: string = Color[6]; // access key with value
Any
Any datatype can be used in places where we are unsure of incoming data while writing the application.The data may be from an external api call or user input.
let apiData: any;
apiData = 'Welcome';
apiData = 123456.99;
apiData = true;
let apiValue: any[];
apiValue = [1, "success", true];
Void
In general, this type of data types are used in functions that do not return any value. In TypeScript void can be assigned to variables but only accepts null
or undefined
.
function noData(): void {
console.log("No data found");
}
Null
You can only assign null
to a null datatype variable.null
is a sub type of all other types so we can assign it to any type.
let nullValue: null = null;
let num: number = nullValue;
Undefined
Undefined is used to store an undefined
datatype, as like null
undefined
is a sub type of all other types so we can assign it to any other types.
let empty: undefined = undefined;
Never
Never type is used to represent the values that never occur.Never is a subtype, can be assigned to any other type. But no other type is assignable to never
function throwError(message: string): never {
throw new Error(message);
}
Thanks for reading this article. The above mentioned are the Data Types in TypeSript used to achieve more clean JavaScript code.
The post What Are the Basic Data Types in TypeSript? appeared first on BLEEDBYTES.
Top comments (1)
Nice article.