| 9417 | * @since 3.10.0 |
| 9418 | */ |
| 9419 | export function ReadonlyMap<Key extends Constraint, Value extends Constraint>( |
| 9420 | key: Key, |
| 9421 | value: Value |
| 9422 | ): $ReadonlyMap<Key, Value> { |
| 9423 | const schema = declareConstructor< |
| 9424 | globalThis.ReadonlyMap<Key["Type"], Value["Type"]>, |
| 9425 | globalThis.ReadonlyMap<Key["Encoded"], Value["Encoded"]>, |
| 9426 | ReadonlyMapIso<Key, Value> |
| 9427 | >()( |
| 9428 | [key, value], |
| 9429 | ([key, value]) => { |
| 9430 | const array = ArraySchema(Tuple([key, value])) |
| 9431 | return (input, ast, options) => { |
| 9432 | if (input instanceof globalThis.Map) { |
| 9433 | return Effect.mapBothEager( |
| 9434 | SchemaParser.decodeUnknownEffect(array)([...input], options), |
| 9435 | { |
| 9436 | onSuccess: (array: ReadonlyArray<readonly [Key["Type"], Value["Type"]]>) => new globalThis.Map(array), |
| 9437 | onFailure: (issue) => SchemaIssue.makeCompositeAtKey(ast, "entries", issue, input, options) |
| 9438 | } |
| 9439 | ) |
| 9440 | } |
| 9441 | return Effect.fail(new SchemaIssue.InvalidType(ast, input, options)) |
| 9442 | } |
| 9443 | }, |
| 9444 | { |
| 9445 | representation: { |
| 9446 | id: "effect/schema/ReadonlyMap", |
| 9447 | payload: null |
| 9448 | }, |
| 9449 | expected: "ReadonlyMap", |
| 9450 | toCodec: ([key, value]) => |
| 9451 | link<globalThis.Map<Key["Encoded"], Value["Encoded"]>>()( |
| 9452 | ArraySchema(Tuple([key, value])), |
| 9453 | SchemaTransformation.transform({ |
| 9454 | decode: (entries) => new globalThis.Map(entries), |
| 9455 | encode: (map) => [...map.entries()] |
| 9456 | }) |
| 9457 | ) |
| 9458 | } |
| 9459 | ) |
| 9460 | return make(schema.ast, { key, value }) |
| 9461 | } |
| 9462 | /** |
| 9463 | * Type-level representation returned by {@link ReadonlySet}. |
| 9464 | * |