Alcaeus
Alcaeus copied to clipboard
Strongly-typed collections
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
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?
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