Data Types in TypeScript
TypeScript offers a rich set of data types that can help you write safer and more predictable code. Understanding these types is crucial for leveraging TypeScript's full potential. This guide covers the primary data types available in TypeScript and how to use them.
Understanding TypeScript Data Types
TypeScript extends JavaScript by adding type annotations. Here are some of the fundamental data types in TypeScript:
- Basic Types:
number
,string
,boolean
,void
,null
,undefined
- Object Types:
object
,Array
,Tuple
- Special Types:
any
,never
,unknown
Basic Data Types
Type | Description |
---|---|
number |
Represents both integer and floating-point numbers. |
string |
Used for textual data. |
boolean |
Represents a true or false value. |
void |
Used for functions that do not return a value. |
null |
Represents the absence of value. |
undefined |
Represents a value that has not been initialized. |
Examples:
number
: Represents both integer and floating-point numbers. For instance:let age: number = 25; let temperature: number = 36.6; console.log(age, temperature); // Output: 25 36.6
string
: Used for textual data. Example:let name: string = "Alice"; let greeting: string = `Hello, ${name}!`; console.log(greeting); // Output: Hello, Alice!
boolean
: Represents a true or false value. Example:let isActive: boolean = true; let isCompleted: boolean = false; console.log(isActive, isCompleted); // Output: true false
void
: Used for functions that do not return a value. Example:function logMessage(message: string): void { console.log(message); } logMessage("This is a message"); // Output: This is a message
null
: Represents the absence of value. Example:let n: null = null; console.log(n); // Output: null
undefined
: Represents a value that has not been initialized. Example:let u: undefined = undefined; console.log(u); // Output: undefined
Object Types
Type | Description |
---|---|
object |
Represents a non-primitive type (anything that is not a number, string, boolean, null, or undefined). |
Array |
Represents lists of values of a specific type. |
Tuple |
Allows you to define an array with fixed sizes and types for each element. |
Examples:
object
: Represents a non-primitive type. For instance:let person: object = { name: "John", age: 30 }; console.log(person); // Output: { name: 'John', age: 30 }
Array
: Represents lists of values of a specific type. Example:let numbers: number[] = [1, 2, 3, 4, 5]; let names: string[] = ["Alice", "Bob", "Charlie"]; console.log(numbers, names); // Output: [1, 2, 3, 4, 5] [ 'Alice', 'Bob', 'Charlie' ]
Tuple
: Defines an array with fixed sizes and types. Example:let personTuple: [string, number] = ["Alice", 30]; console.log(personTuple); // Output: [ 'Alice', 30 ]
Special Types
Type | Description |
---|---|
any |
Represents any value and is used when you do not want to enforce a specific type. |
never |
Represents values that never occur. Typically used for functions that throw exceptions or have infinite loops. |
unknown |
A safer alternative to any that requires type checking before performing operations. |
Examples:
any
: Represents any value. For example:let something: any = 42; something = "Now I'm a string"; console.log(something); // Output: Now I'm a string
never
: Represents values that never occur. Example:function throwError(message: string): never { throw new Error(message); } throwError("Something went wrong!"); // Throws an error
unknown
: A safer alternative toany
. Example:let value: unknown = "Hello, TypeScript"; if (typeof value === "string") { console.log(value.toUpperCase()); // Output: HELLO, TYPESCRIPT }
Conclusion
Understanding TypeScript’s data types is fundamental for writing robust and maintainable code. By using these types effectively, you can leverage TypeScript’s static type checking and other features to improve your development process. Whether you’re dealing with basic types or more advanced ones like tuples and enums, TypeScript provides the tools you need to build reliable applications.
Comments
Post a Comment