From 2d95bb9439c018b46cdc7dd21ed640cf46a8d9da Mon Sep 17 00:00:00 2001 From: Philip Dziubinsky Date: Thu, 2 Jan 2025 20:20:50 +0100 Subject: [PATCH 1/4] add TypeScript support with exclusive types snippet and icon --- public/consolidated/_index.json | 4 +++ public/consolidated/typescript.json | 19 +++++++++++++ public/icons/typescript.svg | 8 ++++++ .../typescript/helper-types/exclusive-type.md | 27 +++++++++++++++++++ snippets/typescript/icon.svg | 8 ++++++ 5 files changed, 66 insertions(+) create mode 100644 public/consolidated/typescript.json create mode 100644 public/icons/typescript.svg create mode 100644 snippets/typescript/helper-types/exclusive-type.md create mode 100644 snippets/typescript/icon.svg 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..bba7b1c2 --- /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:\n```ts\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-type.md b/snippets/typescript/helper-types/exclusive-type.md new file mode 100644 index 00000000..026020f4 --- /dev/null +++ b/snippets/typescript/helper-types/exclusive-type.md @@ -0,0 +1,27 @@ +--- +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: +```ts +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 @@ + + + + + + + + From d5e6305a792d02cb0801a814ec5370f4d1530988 Mon Sep 17 00:00:00 2001 From: Philip Dziubinsky Date: Thu, 2 Jan 2025 20:24:38 +0100 Subject: [PATCH 2/4] refactor exclusive type definition and improve usage examples --- public/consolidated/typescript.json | 2 +- snippets/typescript/helper-types/exclusive-type.md | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/public/consolidated/typescript.json b/public/consolidated/typescript.json index bba7b1c2..072553f9 100644 --- a/public/consolidated/typescript.json +++ b/public/consolidated/typescript.json @@ -12,7 +12,7 @@ "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:\n```ts\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`" + "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" } ] } diff --git a/snippets/typescript/helper-types/exclusive-type.md b/snippets/typescript/helper-types/exclusive-type.md index 026020f4..cce6da4e 100644 --- a/snippets/typescript/helper-types/exclusive-type.md +++ b/snippets/typescript/helper-types/exclusive-type.md @@ -11,10 +11,9 @@ type Exclusive = T | U extends Record | ({ [P in Exclude]?: never } & U) | ({ [P in Exclude]?: never } & T) : T | U; -``` + # Usage: -```ts type A = { name: string; email?: string; provider?: string }; type B = { name: string; phone?: string; country?: string }; @@ -24,4 +23,4 @@ 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 +``` \ No newline at end of file From c5848bf7900354f8d452bad5fee7ab213a5f6d72 Mon Sep 17 00:00:00 2001 From: Philip Dziubinsky Date: Thu, 2 Jan 2025 22:21:51 +0100 Subject: [PATCH 3/4] rename exclusive-type to exclusive-types --- .../helper-types/{exclusive-type.md => exclusive-types.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename snippets/typescript/helper-types/{exclusive-type.md => exclusive-types.md} (100%) diff --git a/snippets/typescript/helper-types/exclusive-type.md b/snippets/typescript/helper-types/exclusive-types.md similarity index 100% rename from snippets/typescript/helper-types/exclusive-type.md rename to snippets/typescript/helper-types/exclusive-types.md From 7eaa39fc5a0bcfca1a6e8ad8c4046ffe9d38c97d Mon Sep 17 00:00:00 2001 From: Philip Dziubinsky Date: Thu, 2 Jan 2025 22:38:55 +0100 Subject: [PATCH 4/4] # is now // --- public/consolidated/typescript.json | 2 +- snippets/typescript/helper-types/exclusive-types.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/public/consolidated/typescript.json b/public/consolidated/typescript.json index 072553f9..98a0680b 100644 --- a/public/consolidated/typescript.json +++ b/public/consolidated/typescript.json @@ -12,7 +12,7 @@ "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" + "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" } ] } diff --git a/snippets/typescript/helper-types/exclusive-types.md b/snippets/typescript/helper-types/exclusive-types.md index cce6da4e..b9ba62ae 100644 --- a/snippets/typescript/helper-types/exclusive-types.md +++ b/snippets/typescript/helper-types/exclusive-types.md @@ -13,7 +13,7 @@ type Exclusive = T | U extends Record : T | U; -# Usage: +// Usage: type A = { name: string; email?: string; provider?: string }; type B = { name: string; phone?: string; country?: string };