-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
584 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
function printSomething3(arg: string): string { | ||
console.log(arg); | ||
return arg; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { DocumentFactory } from "./DocumentFactory"; | ||
const factory = new DocumentFactory(); | ||
|
||
const wordDoc = factory.createDocument("word"); | ||
wordDoc.content = "Hello, the bird is the word"; | ||
wordDoc.printContent(); | ||
|
||
const excelDoc = factory.createDocument("excel"); | ||
excelDoc.content = "Data1, Data2, Data3"; | ||
excelDoc.printContent(); | ||
|
||
try { | ||
const pdfDoc = factory.createDocument("pdf"); | ||
} catch(error) { | ||
console.log(error); | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,41 @@ | ||
type AvailableDrinks = "Coffee" | "Tea" | "Water" | "Soda"; | ||
type NonCaffeinated = Exclude<AvailableDrinks, "Coffee" | "Tea">; | ||
|
||
type SomeDrinks = Extract<AvailableDrinks, "Coffee" | "Tea" >; | ||
|
||
type MaybeString = string | null | undefined; | ||
type JustString = NonNullable<MaybeString>; // string | ||
|
||
function getString() { | ||
return "hello"; | ||
} | ||
|
||
type MyString = ReturnType<typeof getString>; // string | ||
|
||
class MyClass { | ||
x: number; | ||
y: number; | ||
|
||
constructor(x: number, y: number) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
} | ||
|
||
type ConstructorParams = ConstructorParameters<typeof MyClass>; | ||
|
||
type MyInstance = InstanceType<typeof MyClass>; // MyClass | ||
|
||
function greet(name: string, age: number) { | ||
console.log(`Hello ${name}, you are ${age} years old.`); | ||
} | ||
|
||
type Params = Parameters<typeof greet>; // [string, number] | ||
|
||
function fn(this: string, age: number) { | ||
console.log(this.toUpperCase()); | ||
} | ||
|
||
type ThisParamsType = ThisParameterType<typeof fn>; // string | ||
|
||
const fnWithoutThis: OmitThisParameter<typeof fn> = fn.bind("Hello"); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export interface Person { | ||
export type Person = { | ||
name: string; | ||
age: number; | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import { Person } from './Person'; | ||
|
||
type ReadonlyPerson = { | ||
[Property in keyof Person]: Readonly<Person[Property]>; | ||
} | ||
readonly [Property in keyof Person]: Person[Property]; | ||
}; | ||
|
||
const readPerson: ReadonlyPerson = { | ||
name: "Adnane", | ||
age: 38 | ||
}; | ||
|
||
// readPerson.name = "Another name"; // Read only, so not allowed |
This file contains 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
17 changes: 17 additions & 0 deletions
17
3-mastering-function-overloading/3-2-practical-use-case/log.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
function log(message: string): void; | ||
function log(error: Error): void; | ||
function log(message: string, error: Error): void; | ||
function log(param1: string | Error, param2?: Error): void { | ||
if (typeof param1 === 'string' && param2 instanceof Error) { | ||
console.error(`Error: ${param1}`, param2); | ||
} else if (param1 instanceof Error) { | ||
console.error(param1); | ||
} else { | ||
console.log(param1); | ||
} | ||
} | ||
|
||
// Usage | ||
log("User login successful."); | ||
log(new Error("Failed to connect to database.")); | ||
log("User request failed.", new Error("Session timeout")); |
29 changes: 29 additions & 0 deletions
29
3-mastering-function-overloading/3-2-practical-use-case/userExample.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
interface User { | ||
id: number; | ||
username: string; | ||
email: string; | ||
} | ||
|
||
function getUser(id: number): User; | ||
function getUser(username: string): User[]; | ||
function getUser(id: number, username: string): User; | ||
function getUser(param1: number | string, param2?: string): User | User[] { | ||
if (typeof param1 === 'number' && param2) { | ||
// Fetch user by ID and username | ||
console.log(`Fetching user by ID: ${param1} and username: ${param2}`); | ||
return { id: param1, username: param2, email: "[email protected]" }; // Simulated data | ||
} else if (typeof param1 === 'number') { | ||
// Fetch user by ID | ||
console.log(`Fetching user by ID: ${param1}`); | ||
return { id: param1, username: "user123", email: "[email protected]" }; // Simulated data | ||
} else { | ||
// Fetch users by username | ||
console.log(`Fetching users by username: ${param1}`); | ||
return [{ id: 1, username: param1, email: "[email protected]" }]; // Simulated data | ||
} | ||
} | ||
|
||
// Usage | ||
console.log(getUser(10)); | ||
console.log(getUser("john_doe")); | ||
console.log(getUser(10, "john_doe")); |
File renamed without changes.
This file contains 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
File renamed without changes.
23 changes: 23 additions & 0 deletions
23
6-decorators-and-metadata-reflection/6-1-understanding-decorators/logClassExample.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"use strict"; | ||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); | ||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; | ||
return c > 3 && r && Object.defineProperty(target, key, r), r; | ||
}; | ||
var __metadata = (this && this.__metadata) || function (k, v) { | ||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); | ||
}; | ||
function logClass(target) { | ||
console.log(`Class in use: ${target.name}`); | ||
} | ||
let User = class User { | ||
constructor(name, age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
}; | ||
User = __decorate([ | ||
logClass, | ||
__metadata("design:paramtypes", [String, Number]) | ||
], User); |
This file contains 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
34 changes: 34 additions & 0 deletions
34
6-decorators-and-metadata-reflection/6-1-understanding-decorators/logMethodExample.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"use strict"; | ||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); | ||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; | ||
return c > 3 && r && Object.defineProperty(target, key, r), r; | ||
}; | ||
var __metadata = (this && this.__metadata) || function (k, v) { | ||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); | ||
}; | ||
function logMethod(target, key, descriptor) { | ||
const originalMethod = descriptor.value; | ||
descriptor.value = function (...args) { | ||
const start = performance.now(); | ||
const result = originalMethod.apply(this, args); | ||
const finish = performance.now(); | ||
console.log(`${key} executed in ${finish - start} milliseconds`); | ||
return result; | ||
}; | ||
return descriptor; | ||
} | ||
class MathOperations { | ||
add(x, y) { | ||
return x + y; | ||
} | ||
} | ||
__decorate([ | ||
logMethod, | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", [Number, Number]), | ||
__metadata("design:returntype", Number) | ||
], MathOperations.prototype, "add", null); | ||
const math = new MathOperations(); | ||
math.add(5, 3); // Output: "add executed in X milliseconds" |
Oops, something went wrong.