TypeScript has a set of basic data types that can be used to define variables and constants. These data types are similar to those found in other programming languages, such as JavaScript. Here are the basic data types in TypeScript:
- Number: This data type is used to represent both integer and floating-point numbers. In TypeScript, numbers are represented using the
number
keyword. For example:
let count: number = 10;
let price: number = 9.99;
- String: This data type is used to represent text. In TypeScript, strings are represented using the
string
keyword. For example:
let message: string = "Hello, TypeScript!";
- Boolean: This data type is used to represent true/false values. In TypeScript, booleans are represented using the
boolean
keyword. For example:
let isCompleted: boolean = false;
- Null: This data type is used to represent the absence of any value. In TypeScript, null is represented using the
null
keyword. For example:
let result: null = null;
- Undefined: This data type is used to represent a variable that has not been assigned a value. In TypeScript, undefined is represented using the
undefined
keyword. For example:
let score: undefined = undefined;
- Any: This data type is used to represent any type of value. In TypeScript, any is represented using the
any
keyword. For example:
let value: any = "Hello";
value = 10;