## TypeScript çš„ Type
1. boolean: The Truth of It
TypeScript's **boolean** type is straightforward--just `true` or `false`. It's essential for conditions and decisions in your code.
**Example:**
let isDone: boolean = false;
isDone = true;
2. number: All About Numbers
Whether you're dealing with integers, floating-point numbers, or even hexadecimal, binary, or octal, TypeScript's **number** type has you covered.
**Example:**
let decimal: number = 6;
let hex: number = 0xf00d;
let binary: number = 0b1010;
let octal: number = 0o744;
3. string: Handling Textual Data
Strings in TypeScript are for textual data, whether enclosed in single or double quotes, or even template literals for dynamic content.
**Example:**
let color: string = "blue";
color = 'red';
let fullName: string = `John Doe`;
let sentence: string = `Hello, my name is ${fullName}.`;
4. array: Grouping Values Together
Arrays allow you to work with lists of values. TypeScript supports two syntaxes for defining arrays, giving you the flexibility to choose what suits you best.
**Example:**
```
let list: number[] = [1, 2, 3];
let listGeneric: Array<number> = [1, 2, 3];
```
5. tuple: Fixed Collection of Elements
Tuples are like arrays, but with a fixed number of elements where each element can have a different type.
**Example:**
let x: [string, number];
x = ["hello", 10]; // OK
6. enum: Named Constants
Enums allow you to define a set of named constants, making your code more readable and intent more clear.
**Example:**
enum Color {Red, Green, Blue};
let c: Color = Color.Green;
console.log(c); // Output: 1
7. any: The Flexible Type
For situations where you don't want TypeScript's strict typing, the **any** type lets you opt out. It's great for working with dynamic content, but use it sparingly to avoid losing the benefits of TypeScript's type system.
**Example:**
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // OK, definitely a boolean
8. void: No Value Here
The **void** type is used for functions that do not return a value. It's a way of explicitly declaring the absence of a return type.
**Example:**
function warnUser(): void {
console.log("This is my warning message");
}
9. null and undefined: The Absence of Values
These types represent the absence of a value. They are most commonly used in conjunction with other types or in optional parameters.
**Example:**
let u: undefined = undefined;
let n: null = null;
10. never: The Impossible Type
The **never** type represents values that never occur, like a function that always throws an error or an infinite loop. It's used to indicate unreachable code.
**Example:**
function error(message: string): never {
throw new Error(message);
}
## Functions
### Basic Function Syntax
A basic function in TypeScript is similar to a function in JavaScript, but with optional type annotations for the parameters and return type. This helps catch errors during development by ensuring that the correct types are passed to the function.
**Example:**
```
function add(x: number, y: number): number {
return x + y;
}
```
In this example, `x` and `y` are both annotated as `number` types, and the function itself is declared to return a `number`.
### Optional and Default Parameters
- Optional parameters are marked with a `?`
- default values can be assigned directly in the parameter list.
**Example:**
```
function buildName(firstName: string, lastName?: string): string {
if (lastName)
return firstName + " " + lastName;
else
return firstName;
}
function buildNameWithDefault(firstName: string, lastName: string = "Smith"): string {
return firstName + " " + lastName;
}
```
In these examples, `lastName` is optional in the first function and has a default value of `"Smith"` in the second function.
### Rest Parameters
Rest parameters allow you to pass an arbitrary number of arguments to a function. These arguments are collected into an array, providing great flexibility when working with variable numbers of inputs.
**Example:**
```
function sumAll(...numbers: number[]): number {
return numbers.reduce((a, b) => a + b, 0);
}
```
The `sumAll` function can take any number of `number` arguments, and it sums them up using the `reduce` method.