Skip to content

Remove the last three Function.cast double assertions (Endpoint.resource, Resource.extend, Resource.family) #635

Open

Description

@thomasfosterau

Follow-up to #623. Its PR removed every other cast(...) from effect/Function. It kept three, because each one stands for a real double assertion, and the 0.22.0 release freeze ruled out the fixes that would remove them (no change to the emitted .d.ts, no change to runtime behaviour).

  • src/Endpoint.ts resource (return cast(endpoints)): ResourceEndpoints is a non-empty tuple, but endpoints: Array<HttpApiEndpoint.Top> is non-empty only when the options leave some endpoint on. TS rejects a single as with "no match for required element at position 0". The fix is a non-empty guard, Arr.isArrayNonEmpty(endpoints), which throws declarationError when every endpoint is off, followed by a single as with a SAFETY comment. That changes runtime behaviour, since an all-off call returns [] today, so it needs a changeset.
  • src/Resource.ts extend: the spread attribute and relationship maps cannot be proven to be ExtendedAttributes / ExtendedRelationships (Struct.Assign) while the base and extra maps are generic. The same goes for the id and ExtendedId. So make's result cannot be compared with extend's return type. The fix is an internal overload whose signature repeats extend's full generic signature over an erased implementation (extend itself must stay a const so its declaration does not change), or typing the declaration with AsFields-style re-assertions.
  • src/Resource.ts family (const fam: AnyFamily = cast(Object.assign(...))): the object is not a family yet, because relationships is added lazily through Object.defineProperty. Its document / collection are also erased over the generic signatures Family declares. The fix is to build the object with a typed lazy relationships member, for example a class or a typed property descriptor, and type document / collection against Family.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions