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

Support Store generic types to spec types for StoreRecord.data and StoreRecord.id #3691

Open
amcclain opened this issue Jun 12, 2024 · 1 comment

Comments

@amcclain
Copy link
Member

It would be excellent if we could spec an interface for the data element of all records within a given Store, so you would have type-aware support when reading their data in code.

Have not thought it through in detail, but I am assuming StoreRecord would take a generic and then Store, GridModel, etc. would have some way to pass it though. And of course it would need to come out the other side of a variety of StoreRecord-emitting APIs. But I believe it would be a big win, given how common it is to pass records around through an application.

Being able to tell TS that your ids will be either numbers or strings (as opposed to either) could also eliminate some common boilerplate casting.

@amcclain amcclain added the types label Jun 12, 2024
@amcclain amcclain added the ready label Jun 27, 2024
@amcclain amcclain added this to the Top Priorities (DRAFT) milestone Jun 27, 2024
@amcclain
Copy link
Member Author

@haynesjm42 noted that the ability to type record.raw could also be useful. We also discussed the pitfalls of having to carry multiple generics through multiple classes (store, gridmodel, etc) - how to do that in a scalable way.

We could consider a compound type - e.g.

type RecordType<IdType extends StoreRecordId, DataType extends PlainObject, RawType extends PlainObject> = {
    id: IdType;
    data: DataType;
    raw: RawType;
};

class Store<R extends RecordType<any, any, any>> {
    records: R[];
}

// Then in app code....
type PersonRecord = RecordType<number, {name: string, age: number}, {name: string, age: number}>;
const store = new Store<PersonRecord>()
store.records.map(it => it.data.age)

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

No branches or pull requests

1 participant