Skip to content

Commit

Permalink
Merge pull request #142 from px-d/main
Browse files Browse the repository at this point in the history
Add Typescript Category & first initial snippet.
  • Loading branch information
Mathys-Gasnier authored Jan 2, 2025
2 parents b5fd7c7 + 7eaa39f commit 894c2df
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
4 changes: 4 additions & 0 deletions public/consolidated/_index.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,9 @@
{
"lang": "SCSS",
"icon": "/icons/scss.svg"
},
{
"lang": "TYPESCRIPT",
"icon": "/icons/typescript.svg"
}
]
19 changes: 19 additions & 0 deletions public/consolidated/typescript.json
Original file line number Diff line number Diff line change
@@ -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 = T> = T | U extends Record<string, unknown>\n ?\n | ({ [P in Exclude<keyof T, keyof U>]?: never } & U)\n | ({ [P in Exclude<keyof U, keyof T>]?: 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<A, B>;\n\nconst w: EitherOr = { name: \"John\", email: \"[email protected]\" }; // ✅\nconst x: EitherOr = { name: \"John\", phone: \"+123 456\" }; // ✅\nconst y: EitherOr = { name: \"John\", email: \"\", phone: \"\" }; // ⛔️\nconst z: EitherOr = { name: \"John\", phne: \"\", provider: \"\" }; // ⛔️\n"
}
]
}
]
8 changes: 8 additions & 0 deletions public/icons/typescript.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions snippets/typescript/helper-types/exclusive-types.md
Original file line number Diff line number Diff line change
@@ -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 = T> = T | U extends Record<string, unknown>
?
| ({ [P in Exclude<keyof T, keyof U>]?: never } & U)
| ({ [P in Exclude<keyof U, keyof T>]?: never } & T)
: T | U;


// Usage:
type A = { name: string; email?: string; provider?: string };
type B = { name: string; phone?: string; country?: string };

type EitherOr = Exclusive<A, B>;

const w: EitherOr = { name: "John", email: "[email protected]" }; //
const x: EitherOr = { name: "John", phone: "+123 456" }; //
const y: EitherOr = { name: "John", email: "", phone: "" }; // ⛔️
const z: EitherOr = { name: "John", phne: "", provider: "" }; // ⛔️
```
8 changes: 8 additions & 0 deletions snippets/typescript/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 894c2df

Please sign in to comment.