Skip to content

Commit c05109f

Browse files
committedSep 18, 2017
chapter 01: typescript introduction
1 parent 3f39545 commit c05109f

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed
 

‎.eslintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"rules": {
2121
"class-methods-use-this": 0,
2222
"no-plusplus": 0,
23-
"arrow-parens": 0
23+
"arrow-parens": 0,
24+
"no-console": 0
2425
}
2526
}

‎examples/chapter01/typescript/hello-world.js

+22
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,25 @@ var language = 'JavaScript'; // string
66
var favoriteLanguage;
77
var langs = ['JavaScript', 'Ruby', 'Python'];
88
favoriteLanguage = langs[0];
9+
function printName(person) {
10+
console.log(person.name);
11+
}
12+
var john = { name: 'John', age: 21 };
13+
var mary = { name: 'Mary', age: 21, phone: '123-45678' };
14+
printName(john);
15+
printName(mary);
16+
var MyObject = /** @class */ (function () {
17+
function MyObject() {
18+
}
19+
MyObject.prototype.compareTo = function (b) {
20+
if (this.age === b.age) {
21+
return 0;
22+
}
23+
return this.age > b.age ? 1 : -1;
24+
};
25+
return MyObject;
26+
}());
27+
function compareTwoObjects(a, b) {
28+
console.log(a.compareTo(b));
29+
console.log(b.compareTo(a));
30+
}

‎examples/chapter01/typescript/hello-world.ts

+36
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,39 @@ let language = 'JavaScript'; // string
88
let favoriteLanguage: string;
99
let langs = ['JavaScript', 'Ruby', 'Python'];
1010
favoriteLanguage = langs[0];
11+
12+
interface Person {
13+
name: string;
14+
age: number;
15+
}
16+
17+
function printName(person: Person) {
18+
console.log(person.name);
19+
}
20+
21+
const john = { name: 'John', age: 21 };
22+
const mary = { name: 'Mary', age: 21, phone: '123-45678' };
23+
printName(john);
24+
printName(mary);
25+
26+
interface Comparable<T> {
27+
compareTo(b: T): number;
28+
}
29+
30+
class MyObject implements Comparable<MyObject> {
31+
age: number;
32+
33+
compareTo(b: MyObject): number {
34+
if (this.age === b.age) {
35+
return 0;
36+
}
37+
return this.age > b.age ? 1 : -1;
38+
}
39+
}
40+
41+
function compareTwoObjects(a: MyObject, b: MyObject) {
42+
console.log(a.compareTo(b));
43+
console.log(b.compareTo(a));
44+
}
45+
46+

0 commit comments

Comments
 (0)