import { Err, Result } from "../result"; import { Type } from "../type"; export class SetType extends Type> { readonly valueType: Type; constructor(v: Type) { super(); this.valueType = v; } check(val: any): Result> { if(!(val instanceof Set)) return new Err(`${val} is not an instance of Set`); for(const v of val) { const result = this.valueType.check(v); if(result instanceof Err) return new Err(`{val} failed set check on value ${v}: ${result.message}`); } return val as Set; } sliceResult(val: any): Result> { if(!(val instanceof Set)) return new Err(`${val} is not an instance of Set`); const result = new Set(); for(const v of val) { const sliced = this.valueType.sliceResult(v); if(sliced instanceof Err) return new Err(`{val} failed set check on value ${v}: ${sliced.message}`); result.add(sliced); } return result; } } export function set(v: Type): SetType { return new SetType(v); }