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 typings #97

Open
feimosi opened this issue Feb 26, 2019 · 1 comment
Open

TypeScript typings #97

feimosi opened this issue Feb 26, 2019 · 1 comment

Comments

@feimosi
Copy link

feimosi commented Feb 26, 2019

Any chance of getting TypeScript support?

I've already written simple typings (based on react-redux) covering just my use case. I think that's a good start. I don't have currently time to add the whole API.

declare module 'react-component-queries' {
  import { Component, ComponentClass, ComponentType } from 'react';

  export type Matching<InjectedProps, DecorationTargetProps> = {
    [P in keyof DecorationTargetProps]: P extends keyof InjectedProps
      ? InjectedProps[P] extends DecorationTargetProps[P]
        ? DecorationTargetProps[P]
        : InjectedProps[P]
      : DecorationTargetProps[P]
  };

  export type Shared<
    InjectedProps,
    DecorationTargetProps extends Shared<InjectedProps, DecorationTargetProps>
  > = {
    [P in Extract<
      keyof InjectedProps,
      keyof DecorationTargetProps
    >]?: InjectedProps[P] extends DecorationTargetProps[P] ? DecorationTargetProps[P] : never
  };

  export type GetProps<C> = C extends ComponentType<infer P> ? P : never;

  export type ConnectedComponentClass<C, P> = ComponentClass<JSX.LibraryManagedAttributes<C, P>> & {
    WrappedComponent: C;
  };

  export type InferableComponentEnhancerWithProps<TInjectedProps, TNeedsProps> = <
    C extends ComponentType<Matching<TInjectedProps, GetProps<C>>>
  >(
    component: C,
  ) => ConnectedComponentClass<
    C,
    Omit<GetProps<C>, keyof Shared<TInjectedProps, GetProps<C>>> & TNeedsProps
  >;

  export type InferableComponentEnhancer<TInjectedProps> = InferableComponentEnhancerWithProps<
    TInjectedProps,
    {}
  >;

  // <------------------------

  export default function componentQueries<TOwnProps = {}, TStateProps = {}>(
    queries: ({ width }: { width: number }) => TStateProps,
  ): InferableComponentEnhancerWithProps<TStateProps, TOwnProps>;
}
@stevemarksd
Copy link

+1

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