Skip to content

Commit 5e12fe7

Browse files
committed
TS Cheat Sheet
0 parents  commit 5e12fe7

File tree

9 files changed

+259
-0
lines changed

9 files changed

+259
-0
lines changed

classes.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var __extends = (this && this.__extends) || (function () {
2+
var extendStatics = function (d, b) {
3+
extendStatics = Object.setPrototypeOf ||
4+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6+
return extendStatics(d, b);
7+
};
8+
return function (d, b) {
9+
extendStatics(d, b);
10+
function __() { this.constructor = d; }
11+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
12+
};
13+
})();
14+
var User = /** @class */ (function () {
15+
function User(name, email, age) {
16+
this.name = name;
17+
this.email = email;
18+
this.age = age;
19+
console.log('User Created: ' + this.name);
20+
}
21+
User.prototype.register = function () {
22+
console.log(this.name + ' is now registered');
23+
};
24+
User.prototype.payInvoice = function () {
25+
console.log(this.name + ' paid invoice');
26+
};
27+
return User;
28+
}());
29+
var Member = /** @class */ (function (_super) {
30+
__extends(Member, _super);
31+
function Member(id, name, email, age) {
32+
var _this = _super.call(this, name, email, age) || this;
33+
_this.id = id;
34+
return _this;
35+
}
36+
Member.prototype.payInvoice = function () {
37+
_super.prototype.payInvoice.call(this);
38+
};
39+
return Member;
40+
}(User));
41+
//let john = new User('John Doe', 'jdoe@gmail.com', 34);
42+
//console.log(john.age);
43+
//john.register();
44+
var mike = new Member(1, 'Mike Smith', 'mike@gmail.com', 22);
45+
mike.payInvoice();

classes.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
interface UserInterface{
2+
name: string;
3+
email: string;
4+
age: number;
5+
register();
6+
payInvoice();
7+
}
8+
9+
10+
class User implements UserInterface{
11+
name: string;
12+
email: string;
13+
age: number;
14+
15+
constructor(name: string, email: string, age: number){
16+
this.name = name;
17+
this.email = email;
18+
this.age = age;
19+
20+
console.log('User Created: ' + this.name);
21+
}
22+
23+
register(){
24+
console.log(this.name + ' is now registered');
25+
}
26+
27+
payInvoice(){
28+
console.log(this.name + ' paid invoice');
29+
}
30+
31+
}
32+
33+
class Member extends User{
34+
id: number;
35+
36+
constructor(id: number, name: string, email:string, age:number){
37+
super(name, email, age);
38+
this.id = id;
39+
40+
}
41+
42+
payInvoice(){
43+
super.payInvoice();
44+
}
45+
}
46+
47+
//let john = new User('John Doe', 'jdoe@gmail.com', 34);
48+
49+
//console.log(john.age);
50+
//john.register();
51+
52+
let mike: User = new Member(1, 'Mike Smith', 'mike@gmail.com', 22);
53+
mike.payInvoice();

functions.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function getSum(num1, num2) {
2+
return num1 + num2;
3+
}
4+
// console.log(getSum(2, 3));
5+
var mySum = function (num1, num2) {
6+
if (typeof num1 == 'string') {
7+
num1 = parseInt(num1);
8+
}
9+
if (typeof num2 == 'string') {
10+
num2 = parseInt(num2);
11+
}
12+
return num1 + num2;
13+
};
14+
// console.log(mySum(3,5));
15+
function getName(firstName, lastName) {
16+
if (lastName == undefined) {
17+
return firstName;
18+
}
19+
return firstName + ' ' + lastName;
20+
} // lastname é opcional por causa do '?'
21+
console.log(getName('John', 'Doe'));
22+
function myVoid() {
23+
}

functions.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function getSum(num1:number, num2:number):number {
2+
return num1 + num2;
3+
}
4+
5+
// console.log(getSum(2, 3));
6+
7+
let mySum = function(num1:any, num2:any):number{
8+
if(typeof num1 == 'string'){
9+
num1 = parseInt(num1);
10+
}
11+
if(typeof num2 == 'string'){
12+
num2 = parseInt(num2);
13+
}
14+
return num1 + num2;
15+
}
16+
17+
// console.log(mySum(3,5));
18+
19+
function getName(firstName:string, lastName?:string):string{
20+
if(lastName == undefined){
21+
return firstName;
22+
}
23+
return firstName + ' ' + lastName;
24+
} // lastname é opcional por causa do '?'
25+
26+
console.log(getName('John', 'Doe'));
27+
28+
function myVoid():void{
29+
30+
}

index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<title>Page Title</title>
7+
<meta name="viewport" content="width=device-width, initial-scale=1">
8+
<script src="classes.js"></script>
9+
</head>
10+
<body>
11+
TypeScript Page
12+
</body>
13+
</html>

interfaces.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*function showTodo(todo) {
2+
console.log(todo.title + ': ' + todo.text);
3+
}
4+
let myTodo = {
5+
title: 'Titulo tarefa',
6+
text: 'texto tarefa'
7+
};
8+
showTodo(myTodo);
9+
*/
10+
11+
interface Todo{
12+
title: string;
13+
text: string;
14+
}
15+
16+
function showTodo(todo: Todo) {
17+
console.log(todo.title + ': ' + todo.text);
18+
}
19+
20+
myTodo = {
21+
title: 'Titulo tarefa',
22+
text: 'texto tarefa'
23+
};
24+
25+
showTodo(myTodo);

interfaces.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function showTodo(todo: {title: string, text: string}){
2+
console.log(todo.title + ': ' + todo.text);
3+
}
4+
5+
let myTodo = {
6+
title: 'Titulo tarefa',
7+
text: 'texto tarefa'
8+
}
9+
10+
showTodo(myTodo);

types.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var myString;
2+
var myNum;
3+
var myBool;
4+
var myVar;
5+
// let strArray: string[];
6+
// let numArray: number[];
7+
// let boolArray: boolean[];
8+
var strArray;
9+
var numArray;
10+
var boolArray;
11+
var strNumTuple;
12+
myString = 'Hello'.slice(0, 3);
13+
myNum = 22;
14+
myBool = true;
15+
myVar = true;
16+
strArray = ['Hello', 'World'];
17+
numArray = [1, 2, 3];
18+
boolArray = [true, false, false];
19+
strNumTuple = ['hello', 3];
20+
//strNumTuple = ['hello', 3, 4, 5, 'a']; // tambem funciona, só valida os 2 primeiros valores
21+
var myVoid;
22+
var myNull; // pode ser null ou undefined
23+
var myUndefined; // pode ser null ou undefined
24+
myVoid = null; // pode ser undefined também
25+
console.log(strNumTuple);

types.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
let myString: string;
2+
let myNum: number;
3+
let myBool: boolean;
4+
let myVar: any;
5+
6+
// let strArray: string[];
7+
// let numArray: number[];
8+
// let boolArray: boolean[];
9+
10+
let strArray: Array<string>;
11+
let numArray: Array<number>;
12+
let boolArray: Array<boolean>;
13+
14+
let strNumTuple: [string, number];
15+
16+
myString = 'Hello'.slice(0,3);
17+
myNum = 22;
18+
myBool = true;
19+
myVar = true;
20+
21+
strArray = ['Hello', 'World'];
22+
numArray = [1,2,3];
23+
boolArray = [true, false, false];
24+
25+
strNumTuple = ['hello', 3];
26+
27+
//strNumTuple = ['hello', 3, 4, 5, 'a']; // tambem funciona, só valida os 2 primeiros valores
28+
29+
//let myVoid: void;
30+
let myNull: null; // pode ser null ou undefined
31+
let myUndefined: undefined; // pode ser null ou undefined
32+
33+
//myVoid = null; // pode ser undefined também
34+
35+
console.log(strNumTuple);

0 commit comments

Comments
 (0)