Accept fewer `Object`s as `JsDictionary`

This commit is contained in:
Tony Garnock-Jones 2024-03-28 09:57:38 +01:00
parent 3093b89f0d
commit 99d1acdec7
2 changed files with 4 additions and 7 deletions

View File

@ -230,12 +230,7 @@ export namespace Dictionary {
if (typeof x !== 'object' || x === null) return false; if (typeof x !== 'object' || x === null) return false;
switch (x[DictionaryType]) { switch (x[DictionaryType]) {
case 'Dictionary': return true; case 'Dictionary': return true;
case void 0: case void 0: return JsDictionary.isJsDictionary(x);
if (Array.isArray(x)) return false;
if (isEmbedded(x)) return false;
if (Float.isFloat(x)) return false;
if (Bytes.isBytes(x)) return false;
return true;
default: return false; default: return false;
} }
} }

View File

@ -7,7 +7,9 @@ export interface JsDictionary<V> {
export namespace JsDictionary { export namespace JsDictionary {
export function isJsDictionary<V>(x: any): x is JsDictionary<V> { export function isJsDictionary<V>(x: any): x is JsDictionary<V> {
return Dictionary.isDictionary(x) && (x as any)[DictionaryType] === void 0; // We accept only literal objects and objects created via `new Object`
// as dictionaries.
return Object.getPrototypeOf(Object.getPrototypeOf(x ?? false)) === null;
} }
export function from<V>(entries: Iterable<[symbol, V]>): JsDictionary<V> { export function from<V>(entries: Iterable<[symbol, V]>): JsDictionary<V> {