Skip to content

Commit 894c2df

Browse files
Merge pull request #142 from px-d/main
Add Typescript Category & first initial snippet.
2 parents b5fd7c7 + 7eaa39f commit 894c2df

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed

public/consolidated/_index.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,9 @@
3838
{
3939
"lang": "SCSS",
4040
"icon": "/icons/scss.svg"
41+
},
42+
{
43+
"lang": "TYPESCRIPT",
44+
"icon": "/icons/typescript.svg"
4145
}
4246
]

public/consolidated/typescript.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[
2+
{
3+
"categoryName": "Helper Types",
4+
"snippets": [
5+
{
6+
"title": "Exclusive Types",
7+
"description": "Allows to have a type which conforms to either/or.",
8+
"author": "px-d",
9+
"tags": [
10+
"typescript",
11+
"helper-types",
12+
"typedefinition"
13+
],
14+
"contributors": [],
15+
"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"
16+
}
17+
]
18+
}
19+
]

public/icons/typescript.svg

Lines changed: 8 additions & 0 deletions
Loading
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: Exclusive Types
3+
description: Allows to have a type which conforms to either/or.
4+
author: px-d
5+
tags: typescript,helper-types,typedefinition
6+
---
7+
8+
```ts
9+
type Exclusive<T, U = T> = T | U extends Record<string, unknown>
10+
?
11+
| ({ [P in Exclude<keyof T, keyof U>]?: never } & U)
12+
| ({ [P in Exclude<keyof U, keyof T>]?: never } & T)
13+
: T | U;
14+
15+
16+
// Usage:
17+
type A = { name: string; email?: string; provider?: string };
18+
type B = { name: string; phone?: string; country?: string };
19+
20+
type EitherOr = Exclusive<A, B>;
21+
22+
const w: EitherOr = { name: "John", email: "j@d.c" }; //
23+
const x: EitherOr = { name: "John", phone: "+123 456" }; //
24+
const y: EitherOr = { name: "John", email: "", phone: "" }; // ⛔️
25+
const z: EitherOr = { name: "John", phne: "", provider: "" }; // ⛔️
26+
```

snippets/typescript/icon.svg

Lines changed: 8 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)