Skip to content

Commit

Permalink
Replace 'any' with a generic in Set (Close #3337) (#3338)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubk authored Mar 17, 2022
1 parent bf80a88 commit 767baff
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-crabs-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx": minor
---

Replace any with a generic in Set methods
13 changes: 13 additions & 0 deletions packages/mobx/__tests__/v5/base/typescript-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2054,6 +2054,19 @@ test("type inference of the action callback", () => {
}
})

test("TS - type inference of Set", () => {
const set = observable.set<number>()
set.add(1)
set.has(1)
set.delete(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" }
Expand Down
4 changes: 2 additions & 2 deletions packages/mobx/src/types/observableset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class ObservableSet<T = any> implements Set<T>, IInterceptable<ISetWillCh
return this
}

delete(value: any) {
delete(value: T) {
if (hasInterceptors(this)) {
const change = interceptChange<ISetWillChange<T>>(this, {
type: DELETE,
Expand Down Expand Up @@ -202,7 +202,7 @@ export class ObservableSet<T = any> implements Set<T>, IInterceptable<ISetWillCh
return false
}

has(value: any) {
has(value: T) {
this.atom_.reportObserved()
return this.data_.has(this.dehanceValue_(value))
}
Expand Down

0 comments on commit 767baff

Please sign in to comment.