-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
Changeset types should be generic #173
Comments
@vlascik 👋 I might need a little help in understanding a few points. So forgive me for a sec. The return value of stamping out a changeset is a Proxy. Do you see any problems with that? |
Right, I imagine typing proxies could be an issue (never tried it), but seems that people have come up with some workarounds (e.g. here https://stackoverflow.com/questions/59390026/how-to-use-es6-proxy-in-typescript). Can you maybe try to flesh it out, and if you have issues maybe we can summon the gods of typescript at #topic-typescript? :D |
EDIT: Im actually working on improving this as we speak. Here is the current version.
|
@bakerac4 those types were a great help! Thank you. For reference, this is what I ended up with in my // fix changeset types
// taken from https://github.com/validated-changeset/validated-changeset/issues/173
import type Model from '@ember-data/model';
import type { Changeset } from 'ember-changeset';
export type ModelAttributes<T extends Model> = Pick<T, Exclude<keyof T, keyof Model>>;
export type ModelProperties<T extends Model> = Readonly<Pick<T, Exclude<keyof T, keyof ModelAttributes<T>>>>;
export type ChangesetAttributes<T> = T extends Model ? ModelAttributes<T> & ModelProperties<T> : T;
export type BufferedChangeset = ReturnType<typeof Changeset>;
export type GenericChangeset<T> = BufferedChangeset & ChangesetAttributes<T> & { data: T }; and then using it in a component import { Changeset } from 'ember-changeset';
import type MyModel from 'my-app/models/my-model';
import type { GenericChangeset } from 'index';
class EditModel extends Component<PropertyGeneralSignature> {
changeset = Changeset(this.args.myModel) as GenericChangeset<MyModel>; would be great if ember-changeset could have this return type instead. What do you think @snewcomer ? |
It would help the usability of changesets in Typescript if types for changesets were generic, e.g.
BufferedChangeset<UserModel>
, that way we would have the proper type safety and wouldn't have to rely on type casting constantly.The text was updated successfully, but these errors were encountered: