Repository navigation
Expand file tree
/
Copy pathTypeScript Notes.ts
More file actions
146 lines (110 loc) · 3.37 KB
/
Copy pathTypeScript Notes.ts
File metadata and controls
146 lines (110 loc) · 3.37 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
//https://scrimba.com/learn/intrototypescript/typescript-introduction-cEQBWH3
//Variables
//undefined variables are of type any
var example1: boolean = true; //explicit typing
var example2 = 35; //implicit typing
var example3: string;
example3 = 'hello';
let example4: boolean | number; //two types assignable to example4
example4 = true;
example4 = 33;
//Collections
let example5: number[] = [1, 2, 3];
let example6 = [1, 2, 3]; //implicit typing
//There's also the generic array type
let list: Array<number> = [1, 2, 3];
//const for constants
const numLivesForCat = 9;
//numLivesForCat=1; ... error
//Enums
enum CompassDirection { north, east, south, west };
var direction = CompassDirection.east;
//Checking types
class AClass {
name: string;
constructor() {
this.name = 'al';
}
};
let anObject = new AClass();
if (anObject instanceof AClass) { console.log('ok'); } else { console.log('not there yet'); } //[LOG]: 'ok'
//Type Conversion
var variableThatIsNotAString: number = 1;
var i: string = String(variableThatIsNotAString);
var i: string = variableThatIsNotAString.toString();
console.log(typeof (i)); //string
//Arrays
const ex1: string[] = ['hello']; //const arrays must be initialized
let ex2: number[];
ex2 = [1, 2, 3];
const ex3: (number | boolean)[] = [1, 2, true];
let ex4: string[][]; //2D array
//Tuples
const exTuple: [string, number] = ['alan', 59];
//Functions
//These are all the same:
let f1 = function (i: number): number {
return i * i;
}
let f2 = function (i: number) {
return i * i;
}
let f3 = (i: number): number => { return i * i; }
let f4 = (i: number) => { return i * i; }
let f5 = (i: number) => i * i;
//Return type can also be void if nothing's returned
function sayHello(): void {
alert("hello");
}
//Classes
//Class members public by default
//access modifiers (public, private, readonly, protected)
// A class is a special type of JavaScript object which
// is always created via a constructor. These classes
// act a lot like objects, and have an inheritance structure
// similar to languages such as Java/C#/Swift.
class Vendor {
name: string;
constructor(name: string) {
this.name = name;
}
greet() {
return "Hello, welcome to " + this.name;
}
}
// An instance can be created via the new keyword, and
// you can call methods and access properties from the
// object.
const shop = new Vendor("Ye Olde Shop");
console.log(shop.greet());
// You can subclass an object. Here's a food cart which
// has a variety as well as a name:
class FoodTruck extends Vendor {
cuisine: string;
constructor(name: string, cuisine: string) {
super(name);
this.cuisine = cuisine;
}
greet() {
return "Hi, welcome to food truck " + this.name + ". We serve " + this.cuisine + " food.";
}
}
// Correctly passing in two arguments will let you create a
// new instance of the FoodTruck:
const truck = new FoodTruck("Dave's Doritos", "junk");
console.log(truck.greet());
//Interfaces
interface IPerson {
firstName: string;
lastName: string;
}
var me: IPerson;
me.firstName = 'Alan';
me.lastName = 'Colburn';
//can implement multiple interfaces
//inherit all the properties; I don't think they're all req'd though
//let manBearPig: IBear & IMan & IPig;
// Getting Started www.youtube.com / CodingTutorials360
// youtu.be / _QAbWfcCaKc ? t = 1783(TSLint)
// tsc fileName.ts
// @ts-check