Typescript Types

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:1:30

The Any type:

The any type in TypeScript allows variables to have values of any data type. It's often used when the type of a variable is unknown or when working with dynamic data.

Example:

let dynamicValue: any = 42;
dynamicValue = "Hello, TypeScript!";
console.log(dynamicValue); // Outputs: Hello, TypeScript!

bigint:

The bigint type is used to represent large integers in TypeScript. It is especially useful when dealing with numbers that exceed the maximum safe value for JavaScript's number type.

Example:

const bigIntValue: bigint = 1234567890123456789012345678901234567890n;
console.log(bigIntValue); // Outputs the large bigint

boolean:

The boolean type represents a binary value, either true or false.

Example:

let isDone: boolean = false;
console.log(isDone); // Outputs: false

null:

The null type represents an intentional absence of any object value.

Example:

let myValue: null = null;
console.log(myValue); // Outputs: null

number:

The number type is used for numeric values, including integers and floating-point numbers.

Example:

let count: number = 42;
console.log(count); // Outputs: 42

string:

The string type represents textual data, such as words and sentences.

Example:

let greeting: string = "Hello, TypeScript!";
console.log(greeting); // Outputs: Hello, TypeScript!

symbol:

The symbol type is used to create unique and immutable values often used as object property keys.

Example:

const uniqueSymbol = Symbol("description");
console.log(uniqueSymbol); // Outputs: Symbol(description)

undefined:

The undefined type represents a variable that has been declared but hasn't been assigned a value yet.

Example:

let uninitializedValue: undefined = undefined;
console.log(uninitializedValue); // Outputs: undefined

TypeScript Object Types:

TypeScript offers several object types, including object, Array, and user-defined object types.

Example:

const person: { name: string; age: number } = { name: "Alice", age: 30 };

Type Inference:

TypeScript's type inference allows you to omit type annotations, as the compiler infers types from the assigned values.

Example:

let inferredValue = 42; // TypeScript infers 'number' type

Optional Properties:

Optional properties in TypeScript are denoted by a ? symbol, allowing you to define properties that may or may not exist on an object.

Example:

interface Person {
  name: string;
  age?: number; // Optional property
}

Index Signatures:

Index signatures allow you to define object properties with dynamic keys.

Example:

interface Dictionary {
  [key: string]: string;
}
const myDict: Dictionary = { key1: "value1", key2: "value2" }; 

TypeScript Type Annotation:

Type annotation involves explicitly specifying the type of a variable.

Example:

let age: number;
age = 30; // Type annotation for 'age'

Use of Type Annotation as a parameter:

Type annotation can be used for function parameters to specify the expected argument types.

Example:

function greet(name: string): void {
  console.log(Hello, ${name}!);
}
greet("Alice");

Inline Type Annotation:

Inline type annotation is used to specify types directly in expressions or function return types.

Example:

const add: (x: number, y: number) => number = (x, y) => x + y;

TypeScript Type Assertion:

Type assertion is used to tell the TypeScript compiler to treat a value as a specific type.

Example:

let value: any = "Hello";
let strLength: number = (value as string).length;

Using Angular Bracket <>:

Type assertion can also be done using angle brackets in older TypeScript code.

Example:

let value: any = "Hello";
let strLength: number = (<string>value).length;

Using 'as' keyword:

The 'as' keyword is a more modern way of performing type assertion.

Example:

let value: any = "Hello";
let strLength: number = value as string).length;

Type Assertion with object:

Type assertion can be used with objects to specify their type.

Example:

let data: any = { name: "Alice", age: 30 };
let person: { name: string; age: number } = data as { name: string; age: number };

Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this