TypeScript: Basic Type Annotations

TypeScript is a superset of JavaScript and has gained immense popularity in the world of web development. One of its standout features is the ability to add static types to our code, making it more predictable and less prone to runtime errors. At the heart of TypeScript's static typing system lies the concept of type annotations. In this blog post, we will explore TypeScript's basic type annotations.

What are Type Annotations? 

Type Annotations in TypeScript are a way to explicitly tell the TypeScript compiler what type a particular variable should be. This enables the compiler to check for type-related errors at compile time, leading to more robust code. Here's a quick overview of basic type annotations: 

Boolean: Represents true or false values. 

Number: Holds numerical values, including integers, floats, and other numeric literals like hexadecimal and octal. 

String: Used for textual data. 

Array: Represents collections of elements. Can be defined using the type followed by [] or using a generic array type like Array<type>. 

Tuple: A fixed-size array where types of elements are known and can differ. 

Enum: Allows developers to define a set of named constants. 

Any: A versatile type for variables that might be of any type. Useful for transitioning from JavaScript to TypeScript. 

Void: Primarily for functions indicating they don't return any value. 

Null and Undefined: Denote the absence of value. 

Never: Represents values that never occur, typically used for functions that always throw an error or have infinite loops.

Basic Type Annotations with Example

Boolean: 

For true/false values.

let isActive: boolean = true;

Number: 

Both integers and floats. TypeScript also supports hexadecimal, binary, and octal literals.

let decimalValue: number = 10;
let hexValue: number = 0xa3;

String: 

Textual data.
let username: string = "JohnDoe";

Array: 

Collections of elements of the same type.
let list: number[] = [1, 2, 3];
// or using generics
let genericList: Array<number> = [1, 2, 3];

Tuple: 

Fixed-size arrays where element types are known but do not need to be the same.
let userDetails: [string, number] = ["John", 25];

Enum: 

A way of giving more friendly names to sets of numeric values.
enum Directions {North, East, South, West}
let direction: Directions = Directions.North;

Any: 

When you are unsure of a variable's type, useful especially when migrating from JavaScript to TypeScript.
let unknownType: any = "Hello";
unknownType = 5; // This is valid.

Void: 

Indicates the absence of a type, usually in the context of function returns.
function logMessage(): void {
    console.log("Logged!");
}

Null and Undefined: 

Represent the absence of a value or an object.
let u: undefined = undefined;
let n: null = null;

Never: 

Represents a value that never occurs.
function throwError(message: string): never {
    throw new Error(message);
}

Conclusion

In essence, TypeScript's basic type annotations provide a foundational layer for creating more structured and error-resistant code.

Comments