Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strongly-typed collections #244

Open
tpluscode opened this issue Jul 9, 2021 · 2 comments
Open

Strongly-typed collections #244

tpluscode opened this issue Jul 9, 2021 · 2 comments

Comments

@tpluscode
Copy link
Member

tpluscode commented Jul 9, 2021

re HydraCG/Specifications#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

@lambdakris
Copy link

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?

@tpluscode
Copy link
Member Author

tpluscode commented Aug 17, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants