Skip to content

Commit

Permalink
Remove any from ObservableMap public API (Close #3286) (#3287)
Browse files Browse the repository at this point in the history
* Remove any from ObservableMap public API; Add expclicit any to assertion functions

* Add changeset

* Cast to IKeyValueMap
  • Loading branch information
kubk committed Feb 11, 2022
1 parent b4fc9c4 commit 6b92683
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-penguins-pretend.md
@@ -0,0 +1,5 @@
---
"mobx": minor
---

Remove any from 'merge' and 'replace' methods of ObservableMap
1 change: 0 additions & 1 deletion packages/mobx/src/types/legacyobservablearray.ts
Expand Up @@ -85,7 +85,6 @@ class LegacyObservableArray<T> extends StubArray {
let nextIndex = 0
return makeIterable({
next() {
// @ts-ignore
return nextIndex < self.length
? { value: self[nextIndex++], done: false }
: { done: true, value: undefined }
Expand Down
6 changes: 3 additions & 3 deletions packages/mobx/src/types/observablemap.ts
Expand Up @@ -306,14 +306,14 @@ export class ObservableMap<K = any, V = any>
}

/** Merge another object into this object, returns this. */
merge(other: ObservableMap<K, V> | IKeyValueMap<V> | any): ObservableMap<K, V> {
merge(other?: IObservableMapInitialValues<K, V>): ObservableMap<K, V> {
if (isObservableMap(other)) {
other = new Map(other)
}
transaction(() => {
if (isPlainObject(other))
getPlainObjectKeys(other).forEach((key: any) =>
this.set(key as any as K, other[key])
this.set(key as K, (other as IKeyValueMap)[key])
)
else if (Array.isArray(other)) other.forEach(([key, value]) => this.set(key, value))
else if (isES6Map(other)) {
Expand All @@ -332,7 +332,7 @@ export class ObservableMap<K = any, V = any>
})
}

replace(values: ObservableMap<K, V> | IKeyValueMap<V> | any): ObservableMap<K, V> {
replace(values: IObservableMapInitialValues<K, V>): ObservableMap<K, V> {
// Implementation requirements:
// - respect ordering of replacement map
// - allow interceptors to run and potentially prevent individual operations
Expand Down
24 changes: 13 additions & 11 deletions packages/mobx/src/utils/utils.ts
Expand Up @@ -34,7 +34,7 @@ export function warnAboutProxyRequirement(msg: string) {
if (__DEV__ && globalState.verifyProxies) {
die(
"MobX is currently configured to be able to run in ES5 mode, but in ES5 MobX won't be able to " +
msg
msg
)
}
}
Expand All @@ -55,7 +55,7 @@ export function once(func: Lambda): Lambda {
}
}

export const noop = () => { }
export const noop = () => {}

export function isFunction(fn: any): fn is Function {
return typeof fn === "function"
Expand All @@ -80,12 +80,14 @@ export function isObject(value: any): value is Object {
return value !== null && typeof value === "object"
}

export function isPlainObject(value) {
export function isPlainObject(value: any) {
if (!isObject(value)) return false
const proto = Object.getPrototypeOf(value)
if (proto == null) return true
const protoConstructor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
return typeof protoConstructor === "function" && protoConstructor.toString() === plainObjectString
const protoConstructor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor
return (
typeof protoConstructor === "function" && protoConstructor.toString() === plainObjectString
)
}

// https://stackoverflow.com/a/37865170
Expand Down Expand Up @@ -126,11 +128,11 @@ export function createInstanceofPredicate<T>(
} as any
}

export function isES6Map(thing): boolean {
export function isES6Map(thing: any): thing is Map<any, any> {
return thing instanceof Map
}

export function isES6Set(thing): thing is Set<any> {
export function isES6Set(thing: any): thing is Set<any> {
return thing instanceof Set
}

Expand All @@ -139,7 +141,7 @@ const hasGetOwnPropertySymbols = typeof Object.getOwnPropertySymbols !== "undefi
/**
* Returns the following: own enumerable keys and symbols.
*/
export function getPlainObjectKeys(object) {
export function getPlainObjectKeys(object: any) {
const keys = Object.keys(object)
// Not supported in IE, so there are not going to be symbol props anyway...
if (!hasGetOwnPropertySymbols) return keys
Expand All @@ -154,16 +156,16 @@ export const ownKeys: (target: any) => Array<string | symbol> =
typeof Reflect !== "undefined" && Reflect.ownKeys
? Reflect.ownKeys
: hasGetOwnPropertySymbols
? obj => Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj) as any)
: /* istanbul ignore next */ Object.getOwnPropertyNames
? obj => Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj) as any)
: /* istanbul ignore next */ Object.getOwnPropertyNames

export function stringifyKey(key: any): string {
if (typeof key === "string") return key
if (typeof key === "symbol") return key.toString()
return new String(key).toString()
}

export function toPrimitive(value) {
export function toPrimitive(value: any) {
return value === null ? null : typeof value === "object" ? "" + value : value
}

Expand Down

0 comments on commit 6b92683

Please sign in to comment.