Created
February 7, 2025 14:42
-
-
Save Kcko/9fd320df61ce50200850b7ce7c720e9b 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
/* | |
Exclude<T, K> | |
Exclude je utility typ v TypeScriptu, který umožňuje vytvořit nový typ odstraněním určitých hodnot z existujícího typu. Tento typ pracuje především s tzv. union typy (sjednocení). Jeho syntaxe vypadá takto: | |
*/ | |
// 1 | |
type Status = 'active' | 'inactive' | 'pending' | 'deleted'; | |
type VisibleStatus = Exclude<Status, 'deleted'>; | |
let status1: VisibleStatus = 'active'; // OK | |
let status2: VisibleStatus = 'inactive'; // OK | |
let status3: VisibleStatus = 'deleted'; // Error: Type '"deleted"' is not assignable to type 'VisibleStatus'. | |
// 2 | |
type StringOrNull = string | null | undefined; | |
type StringOnly = Exclude<StringOrNull, null | undefined>; | |
const value1: StringOnly = 'Hello'; // OK | |
const value2: StringOnly = null; // Error: Type 'null' is not assignable to type 'StringOnly'. | |
const value3: StringOnly = undefined; // Error: Type 'undefined' is not assignable to type 'StringOnly'. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment