Registry / serialization / ts-map

ts-map

JSON →
library1.0.3jsnpmunverified

ts-map is a TypeScript library that provides a Map-like data structure, mirroring the functionality of the native ES6 Map object but explicitly designed for TypeScript environments. Its current stable version is 1.0.3, with no specific release cadence indicated beyond that. The library aims to offer a familiar API for key-value pair storage, where keys can be of any type (including objects), not just strings, which is a key differentiator from plain JavaScript objects. It supports generics for strong typing of both keys and values, enhancing type safety in TypeScript projects. While it replicates standard Map methods like `set`, `get`, `has`, `delete`, `clear`, `size`, `keys`, `values`, `entries`, and `forEach`, its primary distinction lies in its explicit TypeScript typing and design, which might appeal to developers looking for a fully typed alternative. It's important to note that object keys are compared by reference, mirroring ES6 Map behavior, which is a common gotcha for developers expecting value-based comparison.

npm install ts-map
INSTALL
IMPORT
SIG · TS-MAP
T
ts-map
serializationjavascriptv1.0.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TsMap
import TsMap from 'ts-map'
const TsMap = require('ts-map')
The library primarily uses a default export for the `TsMap` class. It ships with TypeScript types.
TsMap (Type Import)
import type TsMap from 'ts-map'
When only using TsMap as a type (e.g., for type annotations), use an explicit type import.
Constructor with generics
const map = new TsMap<string, number>()
const map = new TsMap()
Always explicitly define generic types for keys and values to leverage full type safety; omitting them defaults to `any`.

Demonstrates basic instantiation, setting various key types, retrieving values, iterating over map entries, and using generics for type safety.

import TsMap from 'ts-map'; // Basic instantiation and usage const map = new TsMap(); const k1: number = 1; const k2: number[] = [2]; const k3: boolean = true; map.set(1, "hello"); map.set(k2, "ts").set(k3, "map"); console.log('Get 1:', map.get(1)); console.log('Get k2:', map.get(k2)); console.log('Size:', map.size); console.log('Keys:', map.keys()); console.log('Values:', map.values()); console.log('-- ForEach --'); map.forEach((value, key) => { console.log(key, ':', value); }); // Constructor with parameter const initialMap = new TsMap<number, string>([ [1, "ok"], [2, "fail"] ]); console.log('Initial map get 1:', initialMap.get(1)); // Class generic usage interface Coder { name: string; } const typedMap = new TsMap<number, Coder>([ [1, {name: 'lavyun'}] ]); typedMap.set(2, {name: "tom"}); // typedMap.set(3, "jack"); // This line would cause a TypeScript error as expected console.log('Typed map get 1:', typedMap.get(1));
Debug
Known issues
gotchaWhen using objects or arrays as keys, `ts-map` (like native ES6 Map) compares them by reference, not by value. Different object instances, even if they have identical contents, will be treated as distinct keys.
fix
Always use the exact same object or array instance when setting, getting, or deleting entries if you intend for them to be the same key. Avoid creating new instances (e.g., `map.get([1])` when `map.set([1], 'value')` was used with a different `[1]` array instance).
affects: >=1.0.0
gotchaOmitting explicit generic type parameters during `TsMap` instantiation will result in the map's keys and values being implicitly typed as `any`, forfeiting TypeScript's type safety benefits.
fix
Always explicitly define generic types for keys and values, for example, `new TsMap<KeyType, ValueType>()`, to ensure the compiler enforces type constraints on map operations.
affects: >=1.0.0
Errors
Common errors & fixes
Argument of type 'string' is not assignable to parameter of type 'Coder'.
Attempting to `set` a value that does not conform to the generic type `V` specified for the `TsMap` instance.
fix
Ensure the value passed to the `set()` method matches the generic type `V` defined during `TsMap` instantiation (e.g., `new TsMap<number, Coder>()` requires `{ name: string }` objects for values).
ReferenceError: TsMap is not defined (or similar CommonJS error).
Attempting to import `ts-map` using CommonJS `require()` syntax in a module that expects ES module syntax, or in an environment where CommonJS is not correctly transpiled/loaded for this package.
fix
Use the ES module import syntax: `import TsMap from 'ts-map';` to correctly import the `TsMap` class.
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
22 hits · last 30 days
node
16
Meta
2
Amazon
1
OpenAI (training)
1
Resources
ts-map — npm install ts-map · libregistry