Typescript Data Types

Typescript is an open-source programming language that is an extension of JavaScript. It is a strongly typed language, which means that it requires a data type to be assigned to a variable or function. This allows the TypeScript compiler to check for errors during development time.

Basic Data Types

Here’s a list of the basic data types in TypeScript:

  • string: Used to represent textual data.
  • number: Used to represent numeric data.
  • boolean: Used to represent true/false values.
  • null: Used to represent an intentional absence of any object value.
  • undefined: Used to represent a variable that has not been assigned a value.

Here’s an example of how to use these basic data types:

“`typescript
let name: string = “John Smith”;
let age: number = 30;
let isMale: boolean = true;
let job: null = null;
let address: undefined = undefined;
“`

Array Data Type

In TypeScript, an array is a collection of items of the same type. Here’s an example of how to create an array:

“`typescript
let numbers: number[] = [1, 2, 3, 4, 5];
let names: string[] = [“John”, “Mary”, “Tom”];
“`

You can also use the Array keyword to create an array:

“`typescript
let numbers: Array = [1, 2, 3, 4, 5];
let names: Array = [“John”, “Mary”, “Tom”];
“`

Tuple Data Type

A tuple is an array with a fixed number of elements where each element may have a different type.

“`typescript
let person: [string, number] = [“John Smith”, 30];
“`

The above example declares a tuple that contains a string and a number.

Enum Data Type

Enum is a type that allows a developer to define a set of named constants. Here’s an example of how to create an enum:

“`typescript
enum DayOfWeek {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}

let day: DayOfWeek = DayOfWeek.Monday;
“`

The above example declares an enum with seven named constants. You can then use the named constants by assigning them to a variable.

Any Data Type

The any data type allows you to assign any value to a variable. It is often used when the type of a variable cannot be known at the time of writing the code.

“`typescript
let value: any = 123;
value = “Hello, world!”;
“`

Void Data Type

The void data type is used to indicate that a function does not return a value.

“`typescript
function logMessage(message: string): void {
console.log(message);
}
“`

The above function logs a message to the console but it does not return anything.

FAQs

Q: What is the difference between null and undefined?

null is used to indicate an intentional non-value or absence of any object value while undefined is used to indicate a situation where a variable has been declared but has not been assigned a value.

Q: What is the use of any data type?

The any data type allows you to assign any value to a variable. It is often used when the type of a variable cannot be known at the time of writing the code.

Q: What is tuple data type used for?

A tuple is an array with a fixed number of elements where each element may have a different type. It is used when you want to represent a fixed set of values as a single entity.

Q: What is the use of the void data type?

The void data type is used to indicate that a function does not return a value.

Similar Posts