Skip to content

Add TypeScript types #30

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

scttdavs
Copy link

No description provided.

@scttdavs scttdavs marked this pull request as ready for review April 24, 2023 14:53
interface Options {
debounce?: boolean,
events?: Record<string, Array<string>>
modelTypes?: Record<string, object>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could probably be more specific than string, to limit it to keyof Models. Up to you if you think that's worth it though.

withRef?: boolean
}

export declare function connectBackboneToReact<Model extends {}, ModelProps extends {}>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming nit: shouldn't this be Models, not Model? It's an object with multiple models in it, right?

}

export declare function connectBackboneToReact<Model extends {}, ModelProps extends {}>(
mapModelToProps: (models: Model, props: ModelProps) => Partial<ModelProps>, options?: Options
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another naming nit: it should be mapModelsToProps

Comment on lines +10 to +14
export declare function connectBackboneToReact<Model extends {}, ModelProps extends {}>(
mapModelToProps: (models: Model, props: ModelProps) => Partial<ModelProps>, options?: Options
): <CombinedProps>(
wrappedComponent: React.ComponentType<CombinedProps>
) => React.ComponentType<Omit<CombinedProps, keyof ModelProps>>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ModelProps might be inconsistent here. As the second param to mapModelsToProps, it should me all of the props passed in from the parent component. But then in the return type you're omitting those props from what needs to be passed in from the parent component.

Compared to what I had written in the JIRA ticket:

function connectBackboneToReact<Models extends {}, ModelProps extends {}>(
  mapModelsToProps: (models: Models) => ModelProps
): <CombinedProps>(
  WrappedComponent: React.ComponentType<CombinedProps>
) => React.ComponentType<Omit<CombinedProps, keyof ModelProps>>;

In that definition, ModelProps was the props specifically being added by mapModelsToProps, so omitting them from the generated component type meant that the parent wouldn't have to pass in anything that was derived from a backbone model. I didn't have a type included for the props param though, so we probably can't just use that as-is. I'm not 100% sure what the best way to write that is where TS can still infer everything properly, tbh.

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

Successfully merging this pull request may close these issues.

2 participants