Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue 41 - change Map to unknown #53

Merged
merged 9 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>>];
});
Loading