Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/softwareventures/dictionary-types

Convenient type definitions for commonly used dictionary/map style objects in TypeScript.
https://github.com/softwareventures/dictionary-types

collections dictionary dictionary-types hashmap map typescript

Last synced: 3 days ago
JSON representation

Convenient type definitions for commonly used dictionary/map style objects in TypeScript.

Awesome Lists containing this project

README

        

# Dictionary Types

Convenient type definitions for commonly used dictionary/map style objects
in TypeScript.

## Deprecated

Since TypeScript 2.1, TypeScript has a built-in `Record` type which renders
this package obsolete. Consider also using an ES6 `Map` instead.

* Instead of `Dictionary`, use `Record` or `Map`.
* Instead of `Dictionary`, use `Record` or `Map`.
* Instead of `ReadonlyDictionary`, use `Readonly>`
or `ReadonlyMap`.
* Instead of `ReadonlyDictionary`, use `Readonly>`
or `ReadonlyMap`.
* Instead of `NumberMap`, use `Record` or `Map`.
* Instead of `ReadonlyNumberMap`, use `Readonly>`
or `ReadonlyMap`.

## Installation and usage

```bash
npm install --save dictionary-types
```

```typescript
import {
Dictionary,
ReadonlyDictionary,
NumberMap,
ReadonlyNumberMap
} from "dictionary-types";
```

## Dictionary\

An object containing elements of type `T`, keyed by `string`.

```typescript
const scores: Dictionary = {
"Amelia": 4,
"Riley": 7,
"April": 5
};

scores["Xander"] = 3;
```

## Dictionary\

An object containing elements of type `TValue`, keyed by `TKey`.

```typescript
const amelia = Symbol();
const riley = Symbol();
const april = Symbol();
const xander = Symbol();

type Participant = typeof amelia | typeof riley | typeof april | typeof xander;

const scores: Dictionary = {
[amelia]: 4,
[riley]: 7,
[april]: 5
};

scores[xander] = 3;
```

## ReadonlyDictionary\

A read-only object containing elements of type `T`, keyed by `string`.

```typescript
function winner(scores: ReadonlyDictionary): string {
let winner = "";
let highScore = 0;

for (const name of Object.keys(scores)) {
if (scores[name] > highScore) {
highScore = scores[name];
winner = name;
}
}

return name;
}
```

## ReadonlyDictionary\

A read-only object containing elements of type `TValue`, keyed by `TKey`.

```typescript
const amelia = Symbol();
const riley = Symbol();
const april = Symbol();
const xander = Symbol();

type Participant = typeof amelia | typeof riley | typeof april | typeof xander;

function winner(scores: ReadonlyDictionary): Participant | null {
let winner: Participant | null = null;
let highScore = 0;

for (const participant of [amelia, riley, april, xander]) {
if (scores[participant] > highScore) {
highScore = scores[participant];
winner = participant;
}
}

return winner;
}
```

## NumberMap\

An object containing elements of type `T`, keyed by `number`.

## ReadonlyNumberMap\

A read-only object containing elements of type `T`, keyed by `number`.

## Copyright

See [LICENSE.md](LICENSE.md).