Alcaeus icon indicating copy to clipboard operation
Alcaeus copied to clipboard

Strongly-typed collections

Open tpluscode opened this issue 3 years ago • 2 comments

re https://github.com/HydraCG/Specifications/issues/233

Alcaeus should also be able to find collections where the member assertions are on the class type, as long as they appear in the ApiDocumentation resource representation.

This should be implemented by the getCollection method

tpluscode avatar Jul 09 '21 05:07 tpluscode

I think I have a question related to this. Currently, how should one access the member field of a resource that is known or expected to be a collection? Currently, I have managed it by explicitly casting the resource.representation.root object to any and accessing member, but I am wondering if there is a more type safe way to do it?

lambdakris avatar Aug 13 '21 14:08 lambdakris

Obviously, there is no "safe" way, meaning that you can never know in runtime that the response will in fact be the collection you expect.

You can force the with type by explicitly setting a generic argument to loadResource. Something along the lines

import { Hydra } from 'alcaeus/web'
import { RdfResource, Collection } from 'alcaeus'

interface MyType extends RdfResource {
}

const collection = Hydra.loadResource<Collection<MyType>>('foo')

Follow this doc page for information on actually implementing the MyType interface

tpluscode avatar Aug 17 '21 17:08 tpluscode