-
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
19 changed files
with
162 additions
and
8 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")); |