-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
1178 lines (728 loc) · 23.8 KB
/
index.ts
File metadata and controls
1178 lines (728 loc) · 23.8 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//? Annotations in ts
/*
Annotations are used to specify the data type of a variable,
parameter, function return value, and other types of values.
Annotations help developers catch errors
by allowing them to specify what types of values can be
assigned to a given variable or passed as an argument to a
function.
*/
// String
let myName: string = "Prakhar katiyar";
myName = "I am some other person";
// console.log(myName);
// Number
let num: number = 12;
num++;
// console.log(num);
// Boolean
let isHard: boolean = true;
// console.log(isHard);
//!------------------------------------------------------
//? Type Inference
/*
Type inference is a feature in TypeScript that allows the
compiler to— determine the type of a variable
based on its value. In other words, if you declare a variable
without explicitly specifying its type, TypeScript will try to
infer the type based on the—to it.
*/
let tech = "Typescript";
let n = 8;
let tsHard = true;
// console.log(typeof (tech)); // string
// console.log(typeof (n)); // number
// console.log(typeof (isHard)); // boolean
//!------------------------------------------------------
//? Any Type
/*
TypeScript has a special any type that can be used to
represent any type. When a variable is annotated with the
any type, TypeScript will allow it to have—and
disable all type checking for that variable and its properties.
*/
//! Any Type (WARNING)
/*
While the any type can be useful in certain situations, it
should be used sparingly. Overuse of any can lead to {untyped code}
and make it harder to catch type-related
bugs during development. It's generally better to use more
specific types whenever possible to get the benefits of
TypeScript's type checking.
*/
let color : any = "crimson";
// console.log(color);
color = 20
// console.log(color);
color = false
// console.log(color);
// color(); // give error on run but not here we can make it a function
//!------------------------------------------------------
//? Function parameters Annotations
/*
Function parameter annotations in TypeScript are used to
specify the expected types Of the parameters that a
function takes.
*/
//? Regular function
function oddOne(num: number) {
return num + 1;
}
// console.log(oddOne(3)); //* 4
//? Arrow functions
const double = (x: number, y : number) => x * y;
// if we do not specify anything as x, y
// it would we assign any to it
// console.log(double(2, 10)); //* 20
// console.log(double(2)); //! error
// console.log(double(2, 4, 5)); //! error
//? Default params value
function greet(person: string = "Anonymous") {
return `Hello @${person}`;
}
// console.log(greet()); //* Hello @Anonymous
// console.log(greet("Prakhar")); //* Hello @Prakhar
//? Return Annotation (Regular)
// Regular function return
function general(param: string) : string {
return param + "Sir";
}
// console.log(general("Prakhar")); //* PrakharSir
// Arrow function
const double2 = (x: number, y: number) : number => x * y;
// console.log(double2(5, 8)); //* 40
//!------------------------------------------------------
//? Void In TypeScript
/*
Void is a type that represents the absence of any value.
It is often used as the return type for functions that do
not return a value.
*/
function printMsg(msg : string) : void {
console.log(`This is my -> ${msg}`);
}
// printMsg("I Love You"); //* This is my -> I Love You
//!------------------------------------------------------
//? Never
/*
The never keyword is used to indicate that a function will
not return anything or that a variable can never have a
value. The never type is useful for indicating that certain
code paths should never be reached, or that certain
values are impossible. It can help catch errors at
compile-time instead of runtime.
*/
// A function that has an infinity loop
function infiniteLoop() : never {
while(true) {};
}
// A variable that can never have a value
let x : never;
function neverReturn() : never {
while(true) {};
}
// x = neverReturn(); //* will never ans
//!------------------------------------------------------
//? Arrays Types
/*
Arrays are a type of object that can store multiple values
of the same data type. Arrays in TypeScript are typed,
which means you can specify the type of values that an
array can hold.
*/
//? 2 Types Of Arrays
/*
- Using the square bracket notation [] to
indicate an array of a specific type
*/
const numbers: number[] = [1, 2, 3, 4];
/*
- Using the generic Array<type> notation to
indicate an array of a specific type
*/
const names : Array<string> = ["Alice", "Bob", "Charlie"]; // old version
//!------------------------------------------------------
//? Multi Dimensional
/*
A mufti-dimensional array is an array that contains other
arrays as its elements. Multi-dimensional arrays can be
defined using the same notation as one-dimensional
arrays, but with nested square brackets.
*/
const singleDi : number[] = [1, 2, 3, 4];
const doubleDi : number[][] = [[1, 2, 3, 4]];
const tripleDi : number[][][] = [[[1, 2, 3, 4]]];
//!------------------------------------------------------
//? Objects
/*
An object in TypeScript is a structured data type that represents
a collection of properties, each with a key and an associated
value. The properties Of an Object can have specific types and
the object itself can be annotated with a type, often defined
using an interface or a type alias. TypeScript uses structural
typing, meaning that the shape of an object (its structure or
properties) is what matters for type compatibility.
*/
//todo: Syntax: type varName (annotations/types) = {property:vale }
// Define a person object
const person: {firstName: string; lastName: string; age: number} = {
firstName: 'John',
lastName: 'Doe',
age: 54
}
// console.log(person.firstName); //* John
// console.log(person.lastName); //* Doe
// console.log(person.age); //* 54
//------------------------------------------------------
function printUser() : {name: string; age: number; location: string} {
return {
name: 'Prakhar',
age: 24,
location: 'china'
}
}
// console.log(printUser()); //* { name: 'Prakhar', age: 24, location: 'china' }
//!------------------------------------------------------
//? Type Aliases
/*
A type alias is a way to create a new name for an existing type. It
allows you to define a custom type that refers to another type and
give it a more meaningful or descriptive name.
Type aliases are defined using the type keyword, followed by the
name of the alias, an equal sign (=), and the type it refers to.
*/
type User = { // convention is uppercase
name: string
age: number
};
const printUserInfo = (user: User) => {
return `Name: (${user.name}) Age: (${user.age})`;
}
// console.log(printUserInfo({ name: "John", age: 23 })); //* Name: (John) Age: (23)
//!------------------------------------------------------
//? Optional Properties
/*
You can make a certain property optional in an
object type by adding a question mark (?) after the
property name.
For example. let's say you have an Object type for a
person with name, age, and email properties. but
you want to make the email property optionaL You
can do this by adding a question mark after the
email property:
*/
type Client = {
name : string;
age: number;
email? : string
}
const clientOne : Client = {
name : "Prakhar",
age: 20,
// Email : optional
}
// console.log(clientOne); //? { name: 'Prakhar', age: 20 }
const clientTwo : Client = {
name : "Prakhar",
age: 20,
email: "Prakhar.katiyar.002@gmail.com"
}
// console.log(clientTwo); //? { name: 'Prakhar', age: 20, email: 'Prakhar.katiyar.002@gmail.com' }
//!------------------------------------------------------
//? Readonly
type ClientPerson = {
name : string;
readonly age: number;
}
const clientPersonOne : ClientPerson = {
name : "Prakhar",
age: 20
}
// clientPersonOne.name = "Omaxe"; //? can possible to change
// clientPersonOne.age = 12 //? not possible cause age is readonly property
//!------------------------------------------------------
//? Intersection Types
/*
An intersection type is a way into a combine multiple types
single type that includes all the properties and methods Of each
constituent type. An intersection type is denoted by the & symbol.
*/
type PersonDetail = {
name: string;
age: number
}
type AccountDetails = {
email: string;
password: number
}
type UserDetails = PersonDetail & AccountDetails;
const userDetailsOne : UserDetails = {
name: "prakhar",
age: 24,
email: "Prakhar@gmail.com",
password: 12345678
}
// console.log(userDetailsOne);
/*
{
name: 'prakhar',
age: 24,
email: 'Prakhar@gmail.com',
password: 12345678
}
*/
//!------------------------------------------------------
//? Unions
/*
Unions are used to declare a type that can have one of several
possible types. Unions are useful when we want to allow a variable
or parameter to accept multiple types
The syntax for defining a union type in TypeScript uses the pipe
symbol (|).
*/
let pass : string | number = 12
pass = "password"
type AccountHolder = PersonDetail | AccountDetails;
const accountHolder1 : AccountHolder = {
name: "Prakhar",
age: 24
}
const accountHolder2 : AccountHolder = {
email: "Pk@gmail.com",
password: 12345,
}
//-----------------------------------------------------
const items : (number | string)[] = [1, 5, 5, "bob"]
// console.log(items); //* [ 1, 5, 5, 'bob' ]
//!------------------------------------------------------
//? Literal Types
/*
Literal types allow you to specify a value that can only be
one specific literal value. This means that a variable with a
literal type can only have one specific value and no other.
*/
let pixColor: "red" | "crimson" | "green";
pixColor = "red";
// pixColor = "yellow" //! error we can't assign it
let secPassword : "secretPassword" = "secretPassword";
// secPassword = "something else" //! error : can't assign anything
//!------------------------------------------------------
//? Tuples
/*
Tuple is a type that represents an array with a fixed number of elements
where each element can have a different type. The order of the types in
the tuple definition corresponds to the order of the values in the actual
array. Tuples are similar to arrays, but they have a specific structure and
can be used to model finite sequences with known lengths.
*/
let myTuple : [number, string] = [2510, "Hello World"]
// console.log(myTuple); //* [ 2510, 'Hello World' ]
//? Destructuring Individual Element
let [first, sec] = myTuple;
// console.log(first); // 2510
// console.log(sec); // Hello World
//!------------------------------------------------------
//? Enum
/*
Enum is a way to define a set of named constants.Enums allow
you to define a collection of related values that can be used
interchangeably in your code.
*/
enum WeatherConditions {
Sunny = "sunny",
Cloudy = "cloudy",
Rainy = "rainy",
}
enum WeekDays {
Sunday,
Monday
}
// console.log(WeekDays.Sunday); //* 0
// console.log(WeatherConditions.Sunny); //* sunny
// console.log(WeekDays); //* { '0': 'Sunday', '1': 'Monday', Sunday: 0, Monday: 1 }
//!-----------------------OOPS------------------------------
//? Class Properties Annotations
/*
You can annotate class properties with a type. This allows you to
define the data type of the property and ensure that it is always
consistent.
*/
class Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name,
this.age = age
}
}
const classPerson = new Person("John", 20);
// console.log(classPerson);
//? Access Modifiers
/*
In TypeScript, you cen use access modifiers to control the visibilit)
of class members (properties and methods). Access modifiers
determine the ways in which class members can be accessed
from within and outside the class.?
*/
//- There are three types of access modifiers in TypeScript
//? - Public -> Members marked as public can be accessed from anywhere, both inside and outside the class.
//? - Private -> Members marked as private can only be accessed from within the class they are defined in.
//? - Protected -> Members marked as protected can be accessed from within the class they are defined in, as well as any subclasses that extend the class.
class Animal {
// `public` can be accessed anywhere (default access modifier)
public name: string;
// `private` can only be accessed within the class itself
private age: number;
// `protected` can be accessed within the class and its subclasses
protected species: string;
constructor(name: string, age: number, species: string) {
this.name = name;
this.age = age;
this.species = species;
}
public getInfo(): string {
return `Name: ${this.name}, Age: ${this.age}, Species: ${this.species}`;
}
private getSecret(): string {
return "This is a private method.";
}
}
class Dog extends Animal {
constructor(name: string, age: number, species: string, private breed: string) {
super(name, age, species);
}
public getDetails(): string {
// Accessing public and protected members from the parent class
return `Name: ${this.name}, Species: ${this.species}, Breed: ${this.breed}`;
}
}
const animal = new Animal("Lion", 5, "Panthera leo");
// console.log(animal.name); //* Public: Accessible
// console.log(animal.age); // Error: 'age' is private and not accessible
// console.log(animal.species); // Error: 'species' is protected and not accessible
const dog = new Dog("Buddy", 3, "Canine", "Golden Retriever");
// console.log(dog.getDetails()); //* Public method
//!-----------------------------------------------------
//? Getters & Setters
/*
Getters and setters are used to access and
modify class properties. Getters and setters allow
you to define a property in a class that looks like a
simple variable from the outside but internally has
additional logic for getting and setting the value.
*/
class GetSetPerson {
private _firstName: string;
private _lastName: string;
constructor(firstName: string, lastName: string) {
this._firstName = firstName;
this._lastName = lastName;
}
// Getter for `fullName`
get fullName(): string {
return `${this._firstName} ${this._lastName}`;
}
// Setter for `fullName`
set fullName(name: string) {
const parts = name.split(" ");
if (parts.length !== 2) {
throw new Error("Full name must include first and last name.");
}
this._firstName = parts[0];
this._lastName = parts[1];
}
// Getter for `firstName`
get firstName(): string {
return this._firstName;
}
// Setter for `firstName`
set firstName(value: string) {
if (!value) {
throw new Error("First name cannot be empty.");
}
this._firstName = value;
}
}
const getSetPerson = new GetSetPerson("John", "Doe");
// Using the getter
// console.log(getSetPerson.fullName); //* John Doe
// Using the setter
getSetPerson.fullName = "Jane Smith";
// console.log(getSetPerson.fullName); //* Jane Smith
// Get and set `firstName`
// console.log(getSetPerson.firstName); //* Jane
getSetPerson.firstName = "Alice";
// console.log(getSetPerson.fullName); //* Alice Smith
//!-----------------------------------------------------
//? Interfaces
/*
Interface is a way to define a contract for the shape of an
object. It specifies the properties and their types that an
object must have. Interfaces are a powerful tool for enforcing
a certain structure in your code.
*/
/*
While interfaces are commonly used to define the structure
of objects, they are not limited to just objects. Interfaces in
TypeScript can also be used to describe the shape of
functions and classes.
*/
//? Interface for Functions
interface MathOperation {
(x: number, y : number) : number;
}
// usage
const add : MathOperation = (a, b) => a + b;
const subtract : MathOperation = (a, b) => a - b;
// console.log(add(5, 1)); //* 6
// console.log(subtract(5, 1)); //* 4
//? Interface for Class
interface Vehicle {
start() : void;
stop() : void;
}
// Class implementing the interface
class Car implements Vehicle {
start(): void {
console.log("Car started");
}
stop(): void {
console.log("Card Stopped");
}
}
// usage
const myCar = new Car();
// myCar.start(); //* Car started
// myCar.stop(); //* Card Stopped
//!-----------------------------------------------------
//? Declaration merging
/*
Once an interface is declared, it cannot be directly modified.
However, TypeScript allows what is informally referred to as
"declaration merging" or "interface extension," which is often
misconstrued as "re-opening."
*/
/*
Declaration merging in TypeScript refers to the ability to
extend or augment an existing declaration, including
interfaces. This can be useful when you want to add new
properties or methods to an existing interface without
modifying the original declaration.
*/
// original interface
interface Food {
name: string,
eat() : void
}
// Declaration merging (interface extension)
interface Food {
expire_date: string,
buy(): void
}
const burger : Food = {
name : "Burger",
eat() {
console.log("Eat me...");
},
buy() {
console.log("Buy a Burger from BUrgerKing");
},
expire_date: "15-10-6520"
}
// console.log(burger);
/*
{
name: 'Burger',
eat: [Function: eat],
buy: [Function: buy],
expire_date: '15-10-6520'
}
*/
//!-----------------------------------------------------
//? Generics
/*
In TypeScript, generics allow you to create reusable
components that can work with a variety of types. Generics
make it possible for you to define functions, classes, and
interfaces that can work with different data types without
having to duplicate code.
*/
// Regular functions
const printNum = (n: number, m: number) : void => console.log([n, m]);
const printStr = (n: string, m: string) : void => console.log([n, m]);
const printBool = (n: boolean, m: boolean) : void => console.log([n, m]);
// printNum(2, 4); //* [ 2, 4 ]
// printStr("hello", "world"); //* [ 'hello', 'world' ]
// printBool(true, false); //* [ true, false ]
// Genetic functions
const printAnyDataTypes = <T>(n : T, m : T) : void => {
console.log([n, m]);
}
// printAnyDataTypes<number>(10, 20); //* [ 10, 20 ]
// printAnyDataTypes<string>("Hello", "world"); //* [ 'Hello', 'world' ]
// printAnyDataTypes<boolean>(true, false); //* [ true, false ]
// Examples...
interface Cat {
name: string;
sound: string;
}
/*
?printAnyDataTypes<Cat>(
{name: "kitty", sound: "meow"},
{name: "Billi", sound: "Purr"}
);
*/
/*
[
{ name: 'kitty', sound: 'meow' },
{ name: 'Billi', sound: 'Purr' }
]
*/
function filterArray<T>(array: T[], condition: (item: T) => boolean): T[] {
return array.filter((item) => condition(item))
}
const numberArray: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
/*
console.log(
filterArray<number>(numberArray, (n) => n % 2 === 0)
); */ // * [ 2, 4, 6, 8, 10 ]
const stringArr: string[] = ["ABCD", "EFGH", "IJK", "LMN"]
/*
console.log(
filterArray(stringArr, (word) => word.length > 3)
); */ //* [ 'ABCD', 'EFGH' ]
//? Multiple types of generics
function reversesPair<T, K>(item : T, value: K) : [K, T] {
return [value, item];
}
/*
console.log(
reversesPair<string, number> ("Hello", 210)
); */ //* [ 210, 'Hello' ]
//? Generics Class
class Box<T> {
private val;
constructor(val: T) {
this.val = val;
}
getVal(): T {
return this.val;
}
}
const stringBox = new Box<String>("Hello, Typescript");
// console.log(stringBox.getVal()); //* Hello, Typescript
//!-----------------------------------------------------
//? Type Narrowing
/*
Type narrowing is the process of refining a variable's type within a
conditional block of code. This allows you to write more precise
and type-safe code.
*/
// Define a union type
type MyType = string | number;