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

Replace 'any' with a generic in Set (Close #3337) #3338

Merged
merged 2 commits into from
Mar 17, 2022
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
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)
kubk marked this conversation as resolved.
Show resolved Hide resolved
set.has(1)
set.delete(1)
// @ts-expect-error
set.add("1")
// @ts-expect-error
set.has("1")
// @ts-expect-error
set.delete("1")
})
Copy link
Collaborator Author

@kubk kubk Mar 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nice feature of ts-expect-error is that it throws if there is no TS error on the next line (unlike ts-ignore)


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