Skip to content

Add TypeScript types#30

Open
scttdavs wants to merge 3 commits into
mongodb-js:masterfrom
scttdavs:add-types
Open

Add TypeScript types#30
scttdavs wants to merge 3 commits into
mongodb-js:masterfrom
scttdavs:add-types

Conversation

@scttdavs
Copy link
Copy Markdown

No description provided.

@scttdavs scttdavs marked this pull request as ready for review April 24, 2023 14:53
Comment thread types/index.d.ts
interface Options {
debounce?: boolean,
events?: Record<string, Array<string>>
modelTypes?: Record<string, object>,
Copy link
Copy Markdown
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.

Comment thread types/index.d.ts
withRef?: boolean
}

export declare function connectBackboneToReact<Model extends {}, ModelProps extends {}>(
Copy link
Copy Markdown
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?

Comment thread types/index.d.ts
}

export declare function connectBackboneToReact<Model extends {}, ModelProps extends {}>(
mapModelToProps: (models: Model, props: ModelProps) => Partial<ModelProps>, options?: Options
Copy link
Copy Markdown
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 thread types/index.d.ts
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
Copy Markdown
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