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

Typescript #76

Open
prbxr opened this issue Oct 21, 2020 · 5 comments
Open

Typescript #76

prbxr opened this issue Oct 21, 2020 · 5 comments

Comments

@prbxr
Copy link

prbxr commented Oct 21, 2020

Hello

I am using your wonderful library!
Would you like to update

export declare function model<S extends Obj, K extends keyof S, A extends FnMap = {}, V extends FnMap = {}>(name: string, { initial, actions, views }: ModelArgs<S, A, V>): (obj?: object | undefined) => S & A & V & Model<S>;

to this obj?: S | undefined

export declare function model<S extends Obj, K extends keyof S, A extends FnMap = {}, V extends FnMap = {}>(name: string, { initial, actions, views }: ModelArgs<S, A, V>): (obj?: S | undefined) => S & A & V & Model<S>;

And setting initial as optional, here:

export declare type ModelArgs<S extends Obj, A extends FnMap, V extends FnMap> = {
    initial: () => S;
    actions?: (self: S & Model<S>) => A;
    views?: (self: S & Model<S>) => V;
};
@prbxr
Copy link
Author

prbxr commented Oct 21, 2020

I can do the PR

@prbxr
Copy link
Author

prbxr commented Oct 21, 2020

and K extends keyof S = keyof S

export declare function model<S extends Obj, K extends keyof S = keyof S, A extends FnMap = {}, V extends FnMap = {}>(name: string, { initial, actions, views }: ModelArgs<S, A, V>): (obj?: S | undefined) => S & A & V & Model<S>;

in order to do

export const ProductModel = model<TProduct>('Product', {
    actions: (state) => ({
        setSelected: (isSelected: boolean) => {
            state.isSelected = isSelected
        },
    }),
})

@prbxr
Copy link
Author

prbxr commented Oct 21, 2020

Another problem:
state.getParent() is not typed
is there a way to type it?

@prbxr
Copy link
Author

prbxr commented Oct 21, 2020

views also not typed

@ForsakenHarmony
Copy link
Owner

Hey @prbxr not sure how I feel about making initial optional, that would kinda require obj to be required

I could imagine passing obj to initial if that's something that would help you (you could just pass through at that point)

let example = model('example', { initial: (param: { prop: boolean }) => param });
//                                         ^                            ^ `S` inferred from here
//                                         | something like `Partial<S>`
example({ prop: true });

K extends keyof S = keyof S

sure

Another problem:
state.getParent() is not typed
is there a way to type it?

It's hard, because it's not fixed
You can use state.getParent() as Model<AnotherModel>

views also not typed

Views are not typed?

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