Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/robojones/ts-nice

Some types that make your life easier when using typescript.
https://github.com/robojones/ts-nice

Last synced: 23 days ago
JSON representation

Some types that make your life easier when using typescript.

Awesome Lists containing this project

README

        

# ts-nice

[![CircleCI](https://circleci.com/gh/robojones/ts-nice.svg?style=svg)](https://circleci.com/gh/robojones/ts-nice)

Some types that make your life easier when using typescript.

There are three interfaces that allow you to do the following things:
- Select specific properties from a type ([Select](#select)).
- Make all properties of a type optional ([Optional](#optional)).
- Set all properties of a type to a specific type ([SetAll](#setall)).

## Select

Allows you to select specific properties from an interface / type.

```typescript
import { Select } from 'ts-nice'

interface Example {
a: number
b: string
}

// Select only the key 'a'.
let result: Select

result = { a: 54 } // valid
result = { a: 'asdf' } // invalid
result = { b: 'hi' } // invalid
result = {} // invalid
```

You can also select multiple properties:

```typescript
interface Example {
a: number
b: string
c: boolean
}

// Select the key 'a' and 'b'.
let result = Select
```

## Optional

Allows you to make all properties of an interface / type optional.

```typescript
import { Optional } from 'ts-nice'

interface Example { a: number, b: string }

// Make all properties optional.
let result: Optional

result = {} // valid
result = { a: 45 } // valid
result = { a: 45, b: 'huhn' } // valid
result = { a: true } // invalid
result = { c: 'asdf' } // invalid
```

## SetAll

Allows you to set all properties of an interface to a specific type.

```typescript
import { SetAll } from 'ts-nice'

interface Example { a: number, b: string }

// Change the type of all properties to boolean.
let result: SetAll

result = { a: true, b: false } // valid
result = { a: 45, b: 'hi' } // invalid
result = { a: true } // invalid
```