-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadvance.ts
More file actions
186 lines (137 loc) · 5.21 KB
/
advance.ts
File metadata and controls
186 lines (137 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// TypeScript provides a variety of advanced features that enhance JavaScript by adding static types and other type-related capabilities. Below are some additional TypeScript features that help developers write safer and more maintainable code:
// 1. Enums
// Enums allow you to define a set of named constants.
// enum Direction {
// Up = 1,
// Down,
// Left,
// Right
// }
// let dir: Direction = Direction.Up;
// 2. Namespaces
// Namespaces provide a way to organize and manage code, especially when dealing with large codebases.
// namespace Validation {
// export function validateName(name: string): boolean {
// return name.length > 0;
// }
// export function validateAge(age: number): boolean {
// return age > 0;
// }
// }
// const isValidName = Validation.validateName('John'); // true
// const isValidAge = Validation.validateAge(25); // true
// 3. Intersection Types
// Intersection types allow you to combine multiple types into one.
// interface Person {
// name: string;
// }
// interface Employee {
// employeeId: number;
// }
// type EmployeePerson = Person & Employee;
// let employee: EmployeePerson = {
// name: "Alice",
// employeeId: 123
// };
// 4. Type Guards
// Type guards allow you to create more specific type checks within code blocks.
// function isString(value: any): value is string {
// return typeof value === "string";
// }
// function example(value: string | number) {
// if (isString(value)) {
// console.log("It's a string:", value);
// } else {
// console.log("It's a number:", value);
// }
// }
// 5. Abstract Classes
// Abstract classes are base classes that cannot be instantiated directly and are meant to be extended by other classes.
// abstract class Animal {
// abstract makeSound(): void;
// move(): void {
// console.log("Moving...");
// }
// }
// class Dog extends Animal {
// makeSound(): void {
// console.log("Bark");
// }
// }
// let dog = new Dog();
// dog.makeSound(); // Bark
// dog.move(); // Moving...
// 6. Index Signatures
// Index signatures allow you to define types for dynamic property keys.
// interface StringArray {
// [index: number]: string;
// }
// let myArray: StringArray;
// myArray = ["Bob", "Fred"];
// let myStr: string = myArray[0]; // Bob
// 7. Type Aliases
// Type aliases create new names for types.
// type Name = string;
// type NameOrNumber = Name | number;
// let nameOrNumber: NameOrNumber;
// nameOrNumber = "Alice";
// nameOrNumber = 42;
// 8. Keyof Operator
// The keyof operator creates a union type of the keys of an object.
// interface Person {
// name: string;
// age: number;
// }
// type PersonKeys = keyof Person; // 'name' | 'age'
// 9. Lookup Types
// Lookup types allow you to retrieve the type of a specific property in an object type.
// interface Person {
// name: string;
// age: number;
// }
// type NameType = Person["name"]; // string
// 10. Conditional Types
// Conditional types allow you to define types that depend on a condition.
// type IsString<T> = T extends string ? true : false;
// type Test1 = IsString<string>; // true
// type Test2 = IsString<number>; // false
// 11. Utility Types
// TypeScript includes several utility types for common type transformations.
// Partial: Makes all properties optional.
// Required: Makes all properties required.
// Readonly: Makes all properties read-only.
// Record: Creates a type with a set of properties of a given type.
// Pick: Creates a type by picking a subset of properties.
// Omit: Creates a type by omitting a subset of properties.
// Exclude: Excludes types from a union.
// Extract: Extracts types from a union.
// NonNullable: Excludes null and undefined from a type.
// ReturnType: Gets the return type of a function.
// InstanceType: Gets the instance type of a constructor function.
// interface Person {
// name: string;
// age: number;
// }
// // Partial
// type PartialPerson = Partial<Person>; // { name?: string; age?: number; }
// // Required
// type RequiredPerson = Required<Person>; // { name: string; age: number; }
// // Readonly
// type ReadonlyPerson = Readonly<Person>; // { readonly name: string; readonly age: number; }
// // Record
// type NameRecord = Record<"firstName" | "lastName", string>; // { firstName: string; lastName: string; }
// // Pick
// type NameOnly = Pick<Person, "name">; // { name: string; }
// // Omit
// type AgeOnly = Omit<Person, "name">; // { age: number; }
// 12. Declaration Merging
// Declaration merging allows you to merge multiple declarations into a single entity.
// interface User {
// name: string;
// }
// interface User {
// age: number;
// }
// const user: User = { name: "Alice", age: 25 };
// Summary
// TypeScript offers a rich set of features that extend beyond standard JavaScript, providing powerful tools for creating robust and maintainable code. These advanced features include enums, namespaces, intersection types, type guards, abstract classes, index signatures, type aliases, the keyof operator, lookup types, conditional types, utility types, and declaration merging. Understanding and leveraging these features can significantly improve your TypeScript development experience.