Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/softwareventures/dictionary-types
- Owner: softwareventures
- License: isc
- Created: 2018-11-06T12:22:29.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2024-10-23T20:31:44.000Z (22 days ago)
- Last Synced: 2024-10-24T08:16:45.834Z (22 days ago)
- Topics: collections, dictionary, dictionary-types, hashmap, map, typescript
- Homepage: https://www.npmjs.com/package/dictionary-types
- Size: 1.17 MB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
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).