Understanding TypeScript Data Types

TypeScript is a statically typed language, meaning that every variable in TypeScript has a type. It is important to understand these types to write clean, efficient, and error-free code. This article covers the basic and most commonly used data types i…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Yuya Hirano

TypeScript is a statically typed language, meaning that every variable in TypeScript has a type. It is important to understand these types to write clean, efficient, and error-free code. This article covers the basic and most commonly used data types in TypeScript, as well as a few additional types that are useful to know.

Number: Used to represent numeric values, such as integers or floating-point numbers.

let a: number = 42;
let b: number = 3.14;

String: Used to represent a sequence of characters.

let name: string = "John Doe";

Boolean: Used to represent true or false values.

let isTrue: boolean = true;
let isFalse: boolean = false;

Array: Used to represent a collection of values of the same type.

let numbers: number[] = [1, 2, 3, 4];
let names: string[] = ["John", "Jane", "Jim"];

Tuple: Used to represent a data structure similar to an array with multiple types.

let user: [string, number] = ["John Doe", 32];

Enum: Used to give named identifiers to numbers or strings.

enum Color {Red, Green, Blue};
let c: Color = Color.Green;

Any: Used to represent any TypeScript type.

let value: any = 42;
value = "Hello";

Void: Used to indicate a function that does not return a value.

function sayHello(): void {
    console.log("Hello!");
}

Never: Used to indicate an error or termination state.

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

These are just a few of the additional TypeScript data types that are available. It's important to understand these types and choose the appropriate one for your specific use case to write efficient and maintainable code.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Yuya Hirano


Print Share Comment Cite Upload Translate Updates
APA

Yuya Hirano | Sciencx (2023-02-06T08:03:29+00:00) Understanding TypeScript Data Types. Retrieved from https://www.scien.cx/2023/02/06/understanding-typescript-data-types/

MLA
" » Understanding TypeScript Data Types." Yuya Hirano | Sciencx - Monday February 6, 2023, https://www.scien.cx/2023/02/06/understanding-typescript-data-types/
HARVARD
Yuya Hirano | Sciencx Monday February 6, 2023 » Understanding TypeScript Data Types., viewed ,<https://www.scien.cx/2023/02/06/understanding-typescript-data-types/>
VANCOUVER
Yuya Hirano | Sciencx - » Understanding TypeScript Data Types. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/06/understanding-typescript-data-types/
CHICAGO
" » Understanding TypeScript Data Types." Yuya Hirano | Sciencx - Accessed . https://www.scien.cx/2023/02/06/understanding-typescript-data-types/
IEEE
" » Understanding TypeScript Data Types." Yuya Hirano | Sciencx [Online]. Available: https://www.scien.cx/2023/02/06/understanding-typescript-data-types/. [Accessed: ]
rf:citation
» Understanding TypeScript Data Types | Yuya Hirano | Sciencx | https://www.scien.cx/2023/02/06/understanding-typescript-data-types/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.