Open saulshanabrook opened 5 years ago
I also am having a go at passing in a schemas object of { readonly [id: string]: { readonly [name: string]: AnyField } }
instead of an array of schemas into the Datastore.create
method. That way, we could have type safe get
access` where you just pass in the id, instead of the whole schema. However, I am blocked by https://github.com/microsoft/TypeScript/issues/33084
I also am having a go at passing in a schemas object
This is what I was thinking of previously, but didn't end up researching it. The issue you mentioned was resolved, so does that mean it is possible?
@vidartf yeah it would just require some larger code changes to the datastores API. Does this PR get you the safety you were looking for?
I think the issue with T[number] is that it creates a union type of the schemas in unfortunate ways
That's what I would expect. What would you expect the change type to be? A union seems helpful because then in the changed signal you can use branching to handle the different tables and it will be type safe.
This PR makes the datastore class generic with respect to the schemas it was created with. This means that the
get
method now can verify that the schema you pass is of the same type as the schemas you created the store with.Note that this should produce no change in emitted code, only in the type declarations.
For example, in the code below,
d.get(s2)
will fail, because it has a different type thans
. We need the<const>
type modifier so it types eachid
as the constant string instead of just a string.@vidartf I think you mentioned wanting something like this in one of our earlier meetings, if I am not mistaken.
cc @ian-r-rose @jasongrout
See https://github.com/microsoft/TypeScript/issues/20965 for a discussion of how to get the type of an array, using
T[number]
Original ideas was posted here https://github.com/jupyterlab/jupyterlab/issues/5382#issuecomment-520078772