Skip to content

Instantly share code, notes, and snippets.

@fazt
Created July 29, 2017 04:58
Show Gist options
  • Save fazt/62fd7d6f4c637161fde282a9d4582e07 to your computer and use it in GitHub Desktop.
Save fazt/62fd7d6f4c637161fde282a9d4582e07 to your computer and use it in GitHub Desktop.
Snippets fro a fast typescript course
// you will learn
// error compilations types
console.log("hello world");
// Types
// string
let myString:string;
myString = "Hello" + "World";
myString = myString.slice(0,3);
// number
let myNum: number;
myNum = 22;
// boolean
let myBool: boolean;
myBool = false;
//any
let myVar: any;
myVar = false;
// arrays
let StringArray: string[];
StringArray = ['Hello', 'World'];
const numArray:number[] = [1, 2, 3];
const boolArray: boolean[] = [true, false, false];
// Tuple - an array with defined elements
let strNumTuple: [string, number];
strNumTuple = ['Hello', 7];
strNumTuple = ['Hello', 7, 1, 3];
// void
let myVoid: void = undefined;
let myNull: null = null;
let myundefined: undefined = undefined;
// Functions
function getSum(num1: number, num2:number):number {
return num1 + num2;
}
console.log(getSum(1, 4));
let myConcatenation = function(num1:string, num2:string):string{
return num1 + num2;
}
let mySum = function(num1: number|string, num2: number|string):number {
if(typeof num1 == 'string'){
num1 = parseInt(num1);
}
if(typeof num2 == 'string') {
num2 = parseInt(num2);
}
return num1 + num2;
}
// optional parameter
function getName(firstName:string, lastName?:string):string {
if(lastName == undefined) {
return firstName;
}
return `${firstName} ${lastName}`;
}
// void function
function myVoidFunction():void {
return;
}
/**
* Interfaces
*/
function showTodo(todo: {title: string, text: string}):void{
console.log(`${todo.title} ${todo.text}`);
}
// definition interface
interface iTodo {
title: string;
text:string
}
let myTodo:iTodo = {title: 'Eat Dinner', text: 'just remenber to eat'};
function todoFunction(todo: iTodo):void {
console.log(todo);
}
/**
* Classes - properties + methods
* modifiers - to access to props and methods
*/
interface IUser {
name: string;
email: string;
age: number;
register();
showmeAge();
ageinYears();
payInvoice();
}
class User implements IUser {
// you can add private, public, protected
name: string;
public email: string;
protected age: number;
constructor(
name: string,
email:string,
age: number
) {
this.name = name;
this.email = email;
this.age = age;
console.log(`user ${this.name}`)
}
public register() {
console.log(`${this.name} is now registered`)
}
public showmeAge() {
return this.age;
}
private AgeinYears() {
return this.age + ' years';
}
payInvoice() {
console.log(`${this.name} is now registered`);
}
}
// inheritance
class Member extends User {
id: number;
constructor(id:number, name:string, email: string, age:number){
super(name, email, age);
this.id = id;
}
payInvoice() {
super.payInvoice();
}
}
let john = new User('Joe MacMillan', '[email protected]', 45);
let gordon = new Member('Gordon', 'Clark', '[email protected]', 40);
const result = mySum('2','2');
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment