Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: \"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"
}
]
}
]
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: "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: "" }; // ⛔️
```
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.