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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"prepublish": "npm run compile",
"release": "standard-version"
},
"types": "types/index.d.ts",
"homepage": "http://github.com/mongodb-js/connect-backbone-to-react",
"repository": {
"type": "git",
Expand Down
21 changes: 21 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { type FunctionComponentElement, type ProviderProps, type PropsWithChildren } from "react";

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?

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

): <CombinedProps>(
wrappedComponent: React.ComponentType<CombinedProps>
) => React.ComponentType<Omit<CombinedProps, keyof ModelProps>>;
Comment on lines +10 to +14
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.



interface BackboneProviderProps<Models> {
models: Models
}

export declare function BackboneProvider<Models extends {}>(props: PropsWithChildren<BackboneProviderProps<Models>>): FunctionComponentElement<ProviderProps<Models>>;