From 193b21ea3b8e98eceace500f2f9a80f25e2a58cf Mon Sep 17 00:00:00 2001 From: Gorbachev Egor <7gorbachevm@gmail.com> Date: Thu, 17 Mar 2022 10:12:11 +0300 Subject: [PATCH 1/2] Replace 'any' with a generic in Set (Close #3337) --- .changeset/tall-seas-study.md | 5 +++++ packages/mobx/__tests__/v5/base/typescript-tests.ts | 11 +++++++++++ packages/mobx/src/types/observableset.ts | 4 ++-- 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 .changeset/tall-seas-study.md diff --git a/.changeset/tall-seas-study.md b/.changeset/tall-seas-study.md new file mode 100644 index 000000000..6e1fc6a99 --- /dev/null +++ b/.changeset/tall-seas-study.md @@ -0,0 +1,5 @@ +--- +"mobx": patch +--- + +Replace any with a generic in Set methods diff --git a/packages/mobx/__tests__/v5/base/typescript-tests.ts b/packages/mobx/__tests__/v5/base/typescript-tests.ts index 2ce5ef056..ddde0886c 100644 --- a/packages/mobx/__tests__/v5/base/typescript-tests.ts +++ b/packages/mobx/__tests__/v5/base/typescript-tests.ts @@ -2054,6 +2054,17 @@ test("type inference of the action callback", () => { } }) +test("TS - type inference of Set", () => { + const set = observable.set() + set.add(1) + // @ts-expect-error + set.add("1") + // @ts-expect-error + set.has("1") + // @ts-expect-error + set.delete("1") +}) + test("TS - type inference of observe & intercept functions", () => { const array = [1, 2] const object = { numberKey: 1, stringKey: "string" } diff --git a/packages/mobx/src/types/observableset.ts b/packages/mobx/src/types/observableset.ts index 062829acb..05171775a 100644 --- a/packages/mobx/src/types/observableset.ts +++ b/packages/mobx/src/types/observableset.ts @@ -159,7 +159,7 @@ export class ObservableSet implements Set, IInterceptable>(this, { type: DELETE, @@ -202,7 +202,7 @@ export class ObservableSet implements Set, IInterceptable Date: Thu, 17 Mar 2022 12:58:45 +0300 Subject: [PATCH 2/2] Test more methods --- .changeset/{tall-seas-study.md => fluffy-crabs-exercise.md} | 2 +- packages/mobx/__tests__/v5/base/typescript-tests.ts | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) rename .changeset/{tall-seas-study.md => fluffy-crabs-exercise.md} (78%) diff --git a/.changeset/tall-seas-study.md b/.changeset/fluffy-crabs-exercise.md similarity index 78% rename from .changeset/tall-seas-study.md rename to .changeset/fluffy-crabs-exercise.md index 6e1fc6a99..3b54d3ce7 100644 --- a/.changeset/tall-seas-study.md +++ b/.changeset/fluffy-crabs-exercise.md @@ -1,5 +1,5 @@ --- -"mobx": patch +"mobx": minor --- Replace any with a generic in Set methods diff --git a/packages/mobx/__tests__/v5/base/typescript-tests.ts b/packages/mobx/__tests__/v5/base/typescript-tests.ts index ddde0886c..c65f66736 100644 --- a/packages/mobx/__tests__/v5/base/typescript-tests.ts +++ b/packages/mobx/__tests__/v5/base/typescript-tests.ts @@ -2057,6 +2057,8 @@ test("type inference of the action callback", () => { test("TS - type inference of Set", () => { const set = observable.set() set.add(1) + set.has(1) + set.delete(1) // @ts-expect-error set.add("1") // @ts-expect-error