Typescript
Basic TypeScript Types to learn - Complete Types in TypeScript
TypeScript provides several basic types that can be used to define variables and function parameters. Here are the most commonly used basic types in TypeScript:
- Boolean - represents a boolean value (true or false)
- Number - represents a numeric value (integer or floating-point)
- String - represents a string value (a sequence of characters)
- Array - represents an array of values of a specific type
- Tuple - represents an array with a fixed number of elements of specific types
- Enum - represents a set of named constants
- Any - represents any value, and can be used to disable type checking
- Void - represents the absence of a value, typically used as the return type for functions that do not return a value
- Null - represents a null value
- Undefined - represents an undefined value
These basic types can be used to define variables, function parameters, and return types. TypeScript also provides advanced types such as union types, intersection types, and type aliases that can be used to create more complex type definitions.
¶Start Testing Basic typescript types
Let’s create a file called basic-type.ts
Sure! Here are some examples of each of the basic types in TypeScript:
¶Boolean:
let isCompleted: boolean = true;
let hasErrors: boolean = false;
// Later catch compile time errors
isCompleted = 'false'; // ❌
isCompleted = null; // ❌
isCompleted = '12'; // ❌
isCompleted = '0'; // ❌
isCompleted = '1'; // ❌
isCompleted = true; // ✅
isCompleted = false; // ✅
¶Number:
let age: number = 30;
let price: number = 9.99;
// Later catch compile time errors
number = '20'; // ❌
number = 'true'; // ❌
number = true; // ❌
number = null; // ❌
number = 50.44; // ✅
number = 100; // ✅
¶String:
let name: string = "John";
let message: string = `Hello ${name}!`;
// Later catch compile time errors
name = 20; // ❌
name = 10.1; // ❌
name = true; // ❌
name = null; // ❌
name = "Jhon Doe"; // ✅
name = 'Jhon Doe'; // ✅
name = `Jhon Doe`; // ✅
name = `${firstName} ${lastName}`; // ✅
¶Array:
let numbers: number[] = [1, 2, 3, 4];
let names: string[] = ["John", "Jane", "Bob"];
// Later catch compile time errors
numbers = 'false'; // ❌
names = [1, "Jhon"]; // ❌
names = [null]; // ❌
number = [1, 100, 200.1]; // ✅
names = ["Akash", "Jhon"]; // ✅
¶Tuple:
let person: [string, number] = ["John", 30];
let coordinate: [number, number, number] = [10, 20, 30];
¶Enum:
enum Color {
Red,
Green,
Blue,
}
let favoriteColor: Color = Color.Blue;
¶Void:
function logMessage(message: string): void {
console.log(message);
}
¶Null:
let nullValue: null = null;
// Later catch compile time errors
nullValue = 'false'; // ❌
nullValue = [1, "Jhon"]; // ❌
nullValue = null; // ✅
¶Undefined:
let undefinedValue: undefined = undefined;
// Later catch compile time errors
undefinedValue = 'false'; // ❌
undefinedValue = [1, "Jhon"]; // ❌
undefinedValue = undefined; // ✅
¶Any:
Anything could be assigned to this. This is not a recommended to use this type. If we use this type, means we don’t know the types and it’s bad practice in Typescript.
let value: any = "hello";
value = 123;
value = ["a", "b", "c"];
In each of these examples, we define a variable and assign it a value of a specific basic type. These basic types can be used to create more complex type definitions, and to ensure that our code is more reliable and less prone to errors.
Installing TypeScript Step by step guide for beginner
TypeScript Variable, Annotation and Inference Learning
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development