diff --git a/.changeset/rude-knives-deny.md b/.changeset/rude-knives-deny.md new file mode 100644 index 0000000..9f4fcc3 --- /dev/null +++ b/.changeset/rude-knives-deny.md @@ -0,0 +1,15 @@ +--- +"@total-typescript/ts-reset": minor +--- + +Added a rule, `/map-constructor`, to default `Map` to `Map` 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. diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 84a4b54..18e2fe8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/package.json b/package.json index f6249b6..84ec64e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/entrypoints/map-constructor.d.ts b/src/entrypoints/map-constructor.d.ts new file mode 100644 index 0000000..8196219 --- /dev/null +++ b/src/entrypoints/map-constructor.d.ts @@ -0,0 +1,3 @@ +interface MapConstructor { + new (): Map; +} diff --git a/src/entrypoints/recommended.d.ts b/src/entrypoints/recommended.d.ts index 7e6b2b4..9f8afbe 100644 --- a/src/entrypoints/recommended.d.ts +++ b/src/entrypoints/recommended.d.ts @@ -4,5 +4,6 @@ /// /// /// +/// /// /// diff --git a/src/tests/map-constructor.ts b/src/tests/map-constructor.ts new file mode 100644 index 0000000..2feb251 --- /dev/null +++ b/src/tests/map-constructor.ts @@ -0,0 +1,61 @@ +import { doNotExecute, Equal, Expect } from "./utils"; + +doNotExecute(() => { + const map = new Map(); + + const result = map.get("foo"); + + type test = [Expect>]; +}); + +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>]; +}); + +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>]; +}); + +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>]; +}); + +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>]; +}); + +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>]; +});