Be explicit about conversions fromJS()

This commit is contained in:
Tony Garnock-Jones 2021-02-22 20:00:15 +01:00
parent a69297c3ba
commit 4353d5280e
2 changed files with 12 additions and 15 deletions

View File

@ -25,11 +25,10 @@ export class Dictionary<V, T extends object = DefaultPointer> extends FlexMap<Va
return d;
}
constructor(items?: readonly [any, V][]);
constructor(items?: Iterable<readonly [any, V]>);
constructor(items?: Iterable<readonly [any, V]>) {
const iter = items?.[Symbol.iterator]();
super(canonicalString, iter === void 0 ? void 0 : _iterMap(iter, ([k,v]) => [fromJS(k), v]));
constructor(items?: readonly [Value<T>, V][]);
constructor(items?: Iterable<readonly [Value<T>, V]>);
constructor(items?: Iterable<readonly [Value<T>, V]>) {
super(canonicalString, items);
}
mapEntries<W, R extends object = DefaultPointer>(f: (entry: [Value<T>, V]) => [Value<R>, W]): Dictionary<W, R> {
@ -84,9 +83,8 @@ export class Set<T extends object = DefaultPointer> extends FlexSet<Value<T>> {
return x?.[DictionaryType] === 'Set';
}
constructor(items?: Iterable<any>) {
const iter = items?.[Symbol.iterator]();
super(canonicalString, iter === void 0 ? void 0 : _iterMap<any, Value<T>>(iter, fromJS));
constructor(items?: Iterable<Value<T>>) {
super(canonicalString, items);
}
map<R extends object = DefaultPointer>(f: (value: Value<T>) => Value<R>): Set<R> {

View File

@ -1,14 +1,14 @@
import { Tag } from "./constants";
import { Encoder } from "./codec";
import { PreserveOn } from "./symbols";
import { DefaultPointer, fromJS, is, Value } from "./values";
import { DefaultPointer, is, Value } from "./values";
export const IsPreservesRecord = Symbol.for('IsPreservesRecord');
export class Record<T extends object = DefaultPointer> extends Array<Value<T>> {
readonly label: Value<T>;
constructor(label: Value<T>, fieldsJS: any[]) {
constructor(label: Value<T>, fields: Value<T>[]) {
if (arguments.length === 1) {
// Using things like someRecord.map() involves the runtime
// apparently instantiating instances of this.constructor
@ -20,8 +20,8 @@ export class Record<T extends object = DefaultPointer> extends Array<Value<T>> {
return;
}
super(fieldsJS.length);
fieldsJS.forEach((f, i) => this[i] = fromJS(f));
super(fields.length);
fields.forEach((f, i) => this[i] = f);
this.label = label;
Object.freeze(this);
}
@ -72,11 +72,10 @@ export class Record<T extends object = DefaultPointer> extends Array<Value<T>> {
}
static makeConstructor<T extends object = DefaultPointer>(labelSymbolText: string, fieldNames: string[]): RecordConstructor<T> {
return Record.makeBasicConstructor(Symbol.for(labelSymbolText), fieldNames);
return Record.makeBasicConstructor<T>(Symbol.for(labelSymbolText), fieldNames);
}
static makeBasicConstructor<T extends object = DefaultPointer>(label0: any, fieldNames: string[]): RecordConstructor<T> {
const label = fromJS<T>(label0);
static makeBasicConstructor<T extends object = DefaultPointer>(label: Value<T>, fieldNames: string[]): RecordConstructor<T> {
const arity = fieldNames.length;
const ctor: RecordConstructor<T> = (...fields: any[]): Record<T> => {
if (fields.length !== arity) {