Skip to content

Commit

Permalink
Merge pull request #53 from pnowak/map-unknown-41
Browse files Browse the repository at this point in the history
Fix for issue 41 - change Map to unknown
  • Loading branch information
mattpocock authored May 27, 2024
2 parents ff2551f + 370bce0 commit 8568d22
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 4 deletions.
15 changes: 15 additions & 0 deletions .changeset/rude-knives-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"@total-typescript/ts-reset": minor
---

Added a rule, `/map-constructor`, to default `Map` to `Map<unknown, unknown>` when no arguments are passed to the constructor.

Before, you'd get `any` for both key and value types. Now, the result of `Map.get` is `unknown` instead of `any`:

```ts
const userMap = new Map();

const value = userMap.get("matt"); // value: unknown
```

This now is part of the recommended rules.
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 7
version: 9
- uses: actions/setup-node@v3
with:
node-version: 16.x
node-version: 20.x
cache: "pnpm"

- run: pnpm install --frozen-lockfile
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
"import": "./dist/set-has.mjs",
"default": "./dist/set-has.js"
},
"./map-constructor": {
"types": "./dist/map-constructor.d.ts",
"import": "./dist/map-constructor.mjs",
"default": "./dist/map-constructor.js"
},
"./map-has": {
"types": "./dist/map-has.d.ts",
"import": "./dist/map-has.mjs",
Expand Down
3 changes: 3 additions & 0 deletions src/entrypoints/map-constructor.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface MapConstructor {
new (): Map<unknown, unknown>;
}
1 change: 1 addition & 0 deletions src/entrypoints/recommended.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
/// <reference path="json-parse.d.ts" />
/// <reference path="array-includes.d.ts" />
/// <reference path="set-has.d.ts" />
/// <reference path="map-constructor.d.ts" />
/// <reference path="map-has.d.ts" />
/// <reference path="array-index-of.d.ts" />
61 changes: 61 additions & 0 deletions src/tests/map-constructor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { doNotExecute, Equal, Expect } from "./utils";

doNotExecute(() => {
const map = new Map();

const result = map.get("foo");

type test = [Expect<Equal<typeof result, unknown>>];
});

doNotExecute(() => {
const map = new Map();

map.set("Jessie", { phone: "213-555-1234", address: "123 N 1st Ave" });

const result = map.has("Jessie");

type test = [Expect<Equal<typeof result, boolean>>];
});

doNotExecute(() => {
const map = new Map();

map.set("Jessie", { phone: "213-555-1234", address: "123 N 1st Ave" });

const result = map.get("Jessie");

type test = [Expect<Equal<typeof result, unknown>>];
});

doNotExecute(() => {
const map = new Map();

map.set("Jessie", { phone: "213-555-1234", address: "123 N 1st Ave" });

const result = map.delete("Jessie");

type test = [Expect<Equal<typeof result, boolean>>];
});

doNotExecute(() => {
const map = new Map();

map.set("Jessie", { phone: "213-555-1234", address: "123 N 1st Ave" });
map.set("Hilary", { phone: "617-555-4321", address: "321 S 2nd St" });

const size = map.size;

type testSize = [Expect<Equal<typeof size, number>>];
});

doNotExecute(() => {
const map = new Map();

map.set("Jessie", { phone: "213-555-1234", address: "123 N 1st Ave" });
map.set("Hilary", { phone: "617-555-4321", address: "321 S 2nd St" });

const cleared = map.clear();

type testClear = [Expect<Equal<typeof cleared, void>>];
});

0 comments on commit 8568d22

Please sign in to comment.