Typescript
November 14, 2026 6 min read

Basic Types and Type Annotations

HS
HEMANT SINGH
Technical Writer & Educator

A Type System is a set of rules that determines what kind of values can be assigned to variables, passed to functions, and returned from functions.


For example, JavaScript allows:

let age = 20;
age = "Hemant"; // Allowed in JavaScript

But TypeScript allows us to define the expected type:

let age: number = 20;
age = "Hemant"; // Error

What is Type Annotation?

Type Annotation means explicitly specifying the type of a variable, function parameter, or return value.


Syntax:

let variableName: type = value;

Examples:

let name: string = "Hemant";
let age: number = 32;
let isLogin: boolean = true;

Here:

  1. name must contain a string.
  2. age must contain a number.
  3. isLogin must contain a boolean.


Type Inference

TypeScript can automatically understand the type from the assigned value.


let name = "Hemant";
let age = 32;
let isLogin = true;

TypeScript automatically infers:

name // string
age // number
isLogin // boolean

You don't always need to write types manually.

Difference:


Type Annotation

Type Inference

You specify the type

TypeScript detects the type

let age: number = 20

let age = 20

Explicit

Automatic



2. String

The string type is used to store textual data.

Examples include:

  1. Student names
  2. Email addresses
  3. Phone numbers stored as text
  4. Addresses
  5. Passwords

Syntax

let name: string = "Hemant Singh";

You can use three types of string notation.

A. Double quotes

let name: string = "Hemant Singh";

B. Single quotes

let name: string = 'Hemant Singh';

C. Template literals

let name: string = `Hemant Singh`;

Template literals are especially useful when combining variables with strings.


3. Number

Unlike some other programming languages, TypeScript doesn't have separate int, float, and double types.



Special number values

JavaScript also supports some special numerical values.

let a: number = Infinity;
let b: number = -Infinity;
let c: number = NaN;

BigInt

The bigint type is used to store very large integer values that exceed the safe integer range of JavaScript's number type.


First, understand the limitation of number.

JavaScript's maximum safe integer is:

Number.MAX_SAFE_INTEGER

Output:

9007199254740991


If you need to work with integers larger than this safely, use bigint.


Syntax

let largeNumber: bigint = 89686586986986n;

Notice the n at the end.

Without n, JavaScript treats the value as a number.




Important differences between Number and BigInt

let a: number = 100;
let b: bigint = 200n;
// let result = a + b; // Error

You cannot directly perform arithmetic between number and bigint.

You must convert them first, provided the conversion is safe and appropriate.

let a: number = 100;
let b: bigint = 200n;
let result = BigInt(a) + b;
console.log(result); // 300n

Important points:

  1. BigInt only supports integers, not decimal values.
  2. BigInt cannot be directly mixed with Number in arithmetic.
  3. JSON.stringify does not serialize BigInt by default.


Practical use: Financial calculations involving very large integer quantities, cryptography, and high-precision integer operations.




Symbol

symbol is a primitive data type in JavaScript and TypeScript.

It creates a unique identifier.

Even if two symbols have the same description, they are different values.


Every Symbol is unique

Consider:

let id1: symbol = Symbol("id");
let id2: symbol = Symbol("id");
console.log(id1 === id2); // false


Practical example: Object properties

Suppose you have an object:

let id: symbol = Symbol("id");

let student = {
name: "Hemant",
age: 32,
[id]: 101
};

console.log(student.name);
console.log(student[id]);


Symbol.for()

JavaScript also provides Symbol.for().

Unlike Symbol(), it uses a global symbol registry.

let id1: symbol = Symbol.for("student_id");
let id2: symbol = Symbol.for("student_id");

console.log(id1 === id2);



Null

null represents the intentional absence of a value.

In simple words, you know a variable exists, but currently it has no value.


let username: null = null;

Here, the variable can contain only null.


Practical example

Suppose a student hasn't provided their profile picture.

let profileImage: string | null = null;


Important: Union Type


Notice this:

let username: string | null = "Aman";

The | symbol means OR.

It means username can contain either:

  1. A string
  2. null

Both are valid:



8. Undefined

undefined represents a variable or property that has not been assigned a value.


Syntax

let username: undefined = undefined;

Any

Now let's understand any.

any disables TypeScript's normal type checking for a particular value.

It allows you to assign almost any type of value to a variable.


Syntax

let username: any = "Aman";

Now you can assign different types:

let username: any = "Aman";

username = 100;
username = true;
username = null;
username = undefined;
username = { name: "Hemant" };

A. Why do we need any?

Suppose you receive data from an external API.

You don't know its structure yet.

let response: any = {
name: "Hemant",
age: 32
};

console.log(response.name);
console.log(response.age);


B. Problem with any

Consider:

let username: any = "Hemant";

console.log(username.toUpperCase()); // HEMANT
console.log(username.toFixed(2)); // Runtime error

Why?

Because toFixed() is a number method, but the actual value is a string.

TypeScript doesn't warn you because you used any.



Unknown

unknown is similar to any, but it is safer.

It allows you to store values of any type, but you cannot directly use that value without checking its type.

Syntax

let username: unknown = "Aman";

You can assign different values:

username = 100;
username = true;
username = null;
username = undefined;
username = { name: "Hemant" };

All are allowed.



Why is unknown safer than any?


Consider any:

let value: any = "Hemant";

console.log(value.toFixed(2));

TypeScript doesn't report an error.

Now consider unknown:

let value: unknown = "Hemant";

console.log(value.toFixed(2)); // Error

TypeScript prevents this because it doesn't know whether value is a number.


B. How to use unknown?


First, check the type.

let value: unknown = "Hemant";

if (typeof value === "string") {
console.log(value.toUpperCase());
}

This process is called Type Narrowing.

TypeScript narrows the possible type after checking it.




Practical example: API response

Suppose your Express API receives data from an external source.

You should not blindly trust its structure.

let apiResponse: unknown = '{"name":"Hemant","age":32}';

if (typeof apiResponse === "string") {
let data = JSON.parse(apiResponse);

console.log(data.name);
}




Any vs Unknown



Feature

Any

Unknown

Accepts any value

Yes

Yes

Can assign any value

Yes

Yes

Direct property access

Allowed

Not allowed

Direct method calls

Allowed

Not allowed

Requires type checking

No

Yes

Safety

Less safe

More safe


Prefer unknown over any when you don't know the incoming data type.



Void

void is mainly used as a function return type.

It indicates that a function does not return a meaningful value.

A. Function without return value

function greet(): void {
console.log("Hello Hemant");
}

greet();

C. Can void return undefined?

Yes.

function greet(): void {
console.log("Hello");

return undefined;
}

This is valid.

But returning a number is not:

function greet(): void {
return 100; // Error
}



Never

never represents a value that can never occur.

It is used when a function never completes normally or when TypeScript determines that no possible value remains.



There are two important use cases.


A. Function that never returns

Consider a function that always throws an error.

function throwError(): never {
throw new Error("Something went wrong");
}

This function never completes normally.

It throws an error instead of returning a value.




Important concept: strictNullChecks

One more important concept before completing this module.

In your tsconfig.json:

{
"compilerOptions": {
"strict": true
}
}

When strict is enabled, strictNullChecks is also enabled.


Consider:

let username: string = "Hemant";
username = null; // Error
username = undefined; // Error

But:

let username: string | null = "Hemant";
username = null; // Allowed

This helps prevent many common programming errors.