Skip to content

Commit d1827dd

Browse files
committed
feat: initial commit
0 parents  commit d1827dd

File tree

9 files changed

+3131
-0
lines changed

9 files changed

+3131
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist/
2+
node_modules/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 LeDDGroup
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# typescript-conditional-types
2+
3+
[![npm version](https://img.shields.io/npm/v/typescript-conditional-types.svg)](https://www.npmjs.com/package/typescript-conditional-types)
4+
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
5+
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
6+
7+
Helpers for typescript generic types
8+
9+
## Motivation
10+
11+
Creating complex types with conditional types ( `T extends U ? X : Y` ) could be a little verbose. This package aims to simplify code and make it more readable.
12+
13+
Instead of
14+
15+
```ts
16+
type ComplexType<A> = A extends boolean
17+
? number
18+
: A extends number ? number : string
19+
: string
20+
```
21+
22+
You could write
23+
24+
```ts
25+
type ComplexType<A> = If<
26+
Or<Extends<A, number>, Extends<A, boolean>>,
27+
number,
28+
string
29+
>;
30+
```
31+
32+
## Install
33+
34+
```bash
35+
$ npm install typescript-conditional-types
36+
```
37+
38+
You'll probably want to save it in the _devDependencies_
39+
40+
## Type Helper List
41+
42+
- _If<Condition, Then, Else>_: If _Condition_ is `true` resulting type is _Then_ else _Else_
43+
- _And<A, B>_: `true` if _A_ and _B_ are both `true` else `false`
44+
- _Or<A, B>_: `true` if _A_ or _B_ are `true` else `false`
45+
- _Not<A>_: Negate _A_
46+
- _Extends<A, B>_: `true` if _A_ extends _B_ like in `A extends B ? true : false`
47+
- _Extends<A, B, Then, Else_: Equivalent to `If<Extends<A, B>, Then, Else>`
48+
49+
## Usage Example
50+
51+
```ts
52+
import { If } from "typescript-conditional-types";
53+
54+
type BooleanToString<T extends boolean> = If<T, "true", "false">
55+
56+
BooleanToString<true> // "true"
57+
BooleanToString<false> // "false"
58+
```

0 commit comments

Comments
 (0)