Last active
February 21, 2023 06:58
-
-
Save waelnassaf/f6ef57c74fd9807dec88ea6abd0e801c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//tsc --init | |
//generate a TypeScript configuration file named tsconfig.json. | |
//This file specifies how TypeScript should compile your project | |
//tsc | |
//tsc --watch | |
//Basic Types. | |
let id: number = 5 | |
let company: string = 'Wael Assaf' | |
let isPublished: boolean = true | |
let x: any = "String" | |
//Initializing | |
let age: number | |
//Then giving value | |
age = 25 | |
//Arrays | |
let ids: number[] = [1, 2, 3, 4, 5] | |
let arr: any[] = [69, true, "Damn", []] | |
//Tuple | |
//With a tuple you can specify the types of each of your array's elements. | |
//Every value should fall in its type's place. | |
let person: [number, string, boolean] = [12, "Wael", true] | |
//Tuple Array | |
let employee: [number, string][] | |
employee = [ | |
[1, 'Wael'], | |
[2, 'Kamel'], | |
[3, 'Sarah'], | |
] | |
//Union | |
let pId: string | number | |
pId = 22 | |
pId = "PnMtIdChQN" | |
//ENUMs | |
//By default: 0 - 1 - 2 - 3 - etc.. | |
enum Direction1 { | |
Up = 1, | |
Down, | |
Left, | |
Right | |
} | |
enum Direction2 { | |
Up = 'Up', | |
Down = 'Down', | |
Left = 'Left', | |
Right = 'Right' | |
} | |
//Objects | |
type User = { | |
id: number | |
name: string | |
} | |
const user: User = { | |
id: 1, | |
name: "John" | |
} | |
//Type Assertion. | |
let cid: any = 1 | |
//Two ways to type assert | |
// let customerId = <number>cid | |
let customerId = cid as number | |
//Functions | |
function addNum(x: number, y: number): number { | |
return x + y | |
} | |
// console.log(addNum(10, 25)) | |
//Void Returns | |
function log(msg: number | string): void { | |
console.log(msg) | |
} | |
// log("Wael Good") | |
//Interfaces. | |
interface UserInterface { | |
readonly id: number | |
name: string | |
age?: number | |
} | |
const emp: UserInterface = { | |
id: 1, | |
name: "John" | |
} | |
//Interfaces with Functions. | |
interface MathFunc { | |
(x: number, y: number): number | |
} | |
const add: MathFunc = (x: number, y:number): number => x + y | |
const sub: MathFunc = (x: number, y:number): number => x - y | |
interface PersonInterface { | |
id: number | |
name: string | |
register(): string | |
} | |
//Classes | |
class Person implements PersonInterface { | |
id: number | |
name: string | |
constructor(id:number, name: string) { | |
this.id = id | |
this.name = name | |
} | |
register() { | |
return `${this.name} is now registered.` | |
} | |
} | |
const wael = new Person(1, "Wael") | |
const mike = new Person(2, "Mike") | |
//Subclass | |
class Employee extends Person { | |
position: string | |
constructor(id:number, name: string, position: string) { | |
super(id, name) | |
this.position = position | |
} | |
} | |
const emp1 = new Employee(3, 'Wael', "CEO") | |
// console.log(emp1.name) | |
// console.log(emp1.register()) | |
//Generics | |
//Used to build reusable components. | |
function getArray<T>(items: T[]): T[] { | |
return new Array().concat(items) | |
} | |
let numArray = getArray<number>([1, 2, 3, 4]) | |
let strArray = getArray<string>(['brad', 'John', 'Jill']) | |
// strArray.push(1) // Throws error | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment