Skip to content

Loosing generic type of component while using withStyles() HOC  #1331

@sergeyzwezdin

Description

@sergeyzwezdin

Describe the bug:
I have React & TypeScript project where JSS is used.

Here is a simple component:

import React, { Component } from 'react';

type TestProps<T> = {
	data?: T[];
	onClick?: (item: T) => void;
};

class Test<T> extends Component<TestProps<T>> {
	render() {
		return <></>;
	}
}

export default Test;

If I check the types that compiled from this code it would be:

import { Component } from 'react';
declare type TestProps<T> = {
    data?: T[];
    onClick?: (item: T) => void;
};
declare class Test<T> extends Component<TestProps<T>> {
    render(): JSX.Element;
}
export default Test;

So everything is OK here.

Now I want to add JSS styles here, so I change the component following way:

import React, { Component } from 'react';
import withStyles, { WithStylesProps } from 'react-jss';

import { JssStyle } from 'jss';

type CssStyles = 'root';
type ICssStyles = Record<CssStyles, JssStyle>;

const styles: ICssStyles = {
	root: {
		color: 'red'
	}
};

type TestProps<T> = {
	data?: T[];
	onClick?: (item: T) => void;
} & WithStylesProps<ICssStyles>;

class Test<T> extends Component<TestProps<T>> {
	render() {
		return <>...</>;
	}
}

export default withStyles(styles)(Test);

Once withStyles HOC is applied to the component, we lose T type and it's going to be unknown:

import React from 'react';
import { WithStylesProps } from 'react-jss';
import { JssStyle } from 'jss';
declare type CssStyles = 'root';
declare type ICssStyles = Record<CssStyles, JssStyle>;
declare type TestProps<T> = {
    data?: T[];
    onClick?: (item: T) => void;
} & WithStylesProps<ICssStyles>;
declare const _default: React.ComponentType<Pick<TestProps<unknown>, "data" | "onClick"> & {
    classes?: Partial<Record<"root", string>> | undefined;
}>;
export default _default;

image

Expected behavior:
We should be able to use the generic parameter of the component after applying withStyles HOC.

Versions (please complete the following information):

  • jss: 10.1.1
  • react-jss: 10.1.1
  • Browser [e.g. chrome, safari]: Chrome
  • OS [e.g. Windows, macOS]: macOS

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions