diff --git a/public/consolidated/_index.json b/public/consolidated/_index.json index 2591d382..9a5f7b11 100644 --- a/public/consolidated/_index.json +++ b/public/consolidated/_index.json @@ -38,5 +38,9 @@ { "lang": "SCSS", "icon": "/icons/scss.svg" + }, + { + "lang": "TYPESCRIPT", + "icon": "/icons/typescript.svg" } ] \ No newline at end of file diff --git a/public/consolidated/typescript.json b/public/consolidated/typescript.json new file mode 100644 index 00000000..98a0680b --- /dev/null +++ b/public/consolidated/typescript.json @@ -0,0 +1,19 @@ +[ + { + "categoryName": "Helper Types", + "snippets": [ + { + "title": "Exclusive Types", + "description": "Allows to have a type which conforms to either/or.", + "author": "px-d", + "tags": [ + "typescript", + "helper-types", + "typedefinition" + ], + "contributors": [], + "code": "type Exclusive = T | U extends Record\n ?\n | ({ [P in Exclude]?: never } & U)\n | ({ [P in Exclude]?: never } & T)\n : T | U;\n\n\n// Usage:\ntype A = { name: string; email?: string; provider?: string };\ntype B = { name: string; phone?: string; country?: string };\n\ntype EitherOr = Exclusive;\n\nconst w: EitherOr = { name: \"John\", email: \"j@d.c\" }; // ✅\nconst x: EitherOr = { name: \"John\", phone: \"+123 456\" }; // ✅\nconst y: EitherOr = { name: \"John\", email: \"\", phone: \"\" }; // ⛔️\nconst z: EitherOr = { name: \"John\", phne: \"\", provider: \"\" }; // ⛔️\n" + } + ] + } +] \ No newline at end of file diff --git a/public/icons/typescript.svg b/public/icons/typescript.svg new file mode 100644 index 00000000..c1d6592b --- /dev/null +++ b/public/icons/typescript.svg @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/snippets/typescript/helper-types/exclusive-types.md b/snippets/typescript/helper-types/exclusive-types.md new file mode 100644 index 00000000..b9ba62ae --- /dev/null +++ b/snippets/typescript/helper-types/exclusive-types.md @@ -0,0 +1,26 @@ +--- +title: Exclusive Types +description: Allows to have a type which conforms to either/or. +author: px-d +tags: typescript,helper-types,typedefinition +--- + +```ts +type Exclusive = T | U extends Record + ? + | ({ [P in Exclude]?: never } & U) + | ({ [P in Exclude]?: never } & T) + : T | U; + + +// Usage: +type A = { name: string; email?: string; provider?: string }; +type B = { name: string; phone?: string; country?: string }; + +type EitherOr = Exclusive; + +const w: EitherOr = { name: "John", email: "j@d.c" }; // ✅ +const x: EitherOr = { name: "John", phone: "+123 456" }; // ✅ +const y: EitherOr = { name: "John", email: "", phone: "" }; // ⛔️ +const z: EitherOr = { name: "John", phne: "", provider: "" }; // ⛔️ +``` \ No newline at end of file diff --git a/snippets/typescript/icon.svg b/snippets/typescript/icon.svg new file mode 100644 index 00000000..c1d6592b --- /dev/null +++ b/snippets/typescript/icon.svg @@ -0,0 +1,8 @@ + + + + + + + +