Created
February 7, 2025 14:36
-
-
Save Kcko/0f208c0d83fbb093fa9d31d28c1dc690 to your computer and use it in GitHub Desktop.
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
// 1. Primitivní typy - lze zjednodušit | |
// S generics: | |
function processValue<T extends string>(value: T) { } | |
// Jednodušeji: | |
function processValue(value: string) { } | |
// 2. Union typy - lze zjednodušit | |
type Fruit = "apple" | "banana" | "orange"; | |
// S generics: | |
function processFruit<T extends Fruit>(fruit: T) { } | |
// Jednodušeji: | |
function processFruit(fruit: Fruit) { } | |
// 3. Literal typy - lze zjednodušit | |
// S generics: | |
function processLiteral<T extends "yes" | "no">(answer: T) { } | |
// Jednodušeji: | |
function processLiteral(answer: "yes" | "no") { } | |
// 4. Array typy - lze zjednodušit v základním případě | |
// S generics: | |
function processArray<T extends any[]>(arr: T) { } | |
function processStringArray<T extends string[]>(arr: T) { } | |
// Jednodušeji: | |
function processArray(arr: any[]) { } | |
function processStringArray(arr: string[]) { } | |
// 5. Function typy - lze zjednodušit | |
type Logger = (msg: string) => void; | |
// S generics: | |
function withLogging<T extends Logger>(logger: T) { } | |
// Jednodušeji: | |
function withLogging(logger: Logger) { } | |
// 6. Class typy - lze zjednodušit v základním případě | |
class Animal { } | |
// S generics: | |
function processClass<T extends Animal>(instance: T) { } | |
// Jednodušeji: | |
function processClass(instance: Animal) { } | |
// 7. Generický typ s extends - POUZE PŘES GENERIC CONSTRAINT | |
interface Box<T> { | |
value: T; | |
} | |
interface NumberBox<T extends number> extends Box<T> { | |
increment(): void; | |
} | |
// 8. Kombinace více typů - lze zjednodušit | |
type StringWithLength = string & { length: number }; | |
// S generics: | |
function processString<T extends string & { length: number }>(value: T) { } | |
// Jednodušeji: | |
function processString(value: StringWithLength) { } | |
// 9. Conditional type - POUZE PŘES GENERIC CONSTRAINT | |
type NonNullable<T> = T extends null | undefined ? never : T; | |
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
// S generic constraint | |
interface WithName { | |
name: string; | |
} | |
function getName<T extends WithName>(obj: T): string { | |
return obj.name; | |
} | |
// S utility typem | |
function getName2(obj: Pick<{ name: string }, 'name'>): string { | |
return obj.name; | |
} |
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
// Vlastní utility typ pro objekty s ID | |
type HasId<T> = T & { id: string }; | |
// Místo generic constraint | |
function processEntity<T>(entity: HasId<T>) { | |
console.log(entity.id); | |
return entity; | |
} | |
// Použití: | |
const user = { id: "123", name: "Jan" }; | |
processEntity(user); // OK |
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
// Místo generic constraint můžeme použít Extract | |
type ValidTypes = string | number | boolean; | |
// S generic constraint | |
function processValue<T extends ValidTypes>(value: T) { | |
return value; | |
} | |
// S Extract - dosáhneme podobného výsledku | |
function processValue2(value: Extract<unknown, ValidTypes>) { | |
return value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment