Skip to content

Typing issue with React Higher-Order-Components (JSX elements attributes type 'T' must be an object type) #5887

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

Closed
frankwallis opened this issue Dec 2, 2015 · 2 comments
Assignees
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@frankwallis
Copy link
Contributor

I am having an issue trying to get the right typings flowing through when creating higher order components in React. Consider the following:

import * as React from 'react';
import {Component, ComponentClass, Props} from 'react';

/* higher-order component */
export interface HighlightedProps {
    isHighlighted?: boolean;
}

export function Highlighted<T>(InputTemplate: ComponentClass<T>): ComponentClass<T & HighlightedProps> {
    return class extends Component<T & HighlightedProps, void> {
        constructor(props) {
            super(props);
        }

        render() {
            let className = this.props.isHighlighted ? "highlighted" : "";
            return (
                <div className={className}>
                    <InputTemplate {...this.props}/>
                </div>
            );
        }
    }
}

/* some basic components */
interface MyInputProps {
    inputValue: string;
}
class MyInput extends Component<MyInputProps, void> { };

interface MyLinkProps {
    linkAddress: string;
}
class MyLink extends Component<MyLinkProps, void> { };

/* wrapped components */
const HighlightedInput = Highlighted(MyInput);
const HighlightedLink = Highlighted(MyLink);

/* usage example */
export class Form extends Component<any, void> {
    render() {
        return (
            <div>
                <HighlightedInput inputValue={"inputValue"} isHighlighted={false} />
                <HighlightedLink linkAddress={"/home"} isHighlighted={true} />
            </div>
        );
    }
}

But this errors with JSX elements attributes type 'T' must be an object type. Is this an issue or am I doing something wrong here?

This may also be related to #4362, but I am unsure if it is exactly the same issue or not.

Any help would be much appreciated, thanks.

@RyanCavanaugh RyanCavanaugh self-assigned this Dec 2, 2015
@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Dec 8, 2015
@tejacques
Copy link

I believe this is fixed with #4362:

Using your exact input as src/HOC.tsx

$ tsc --version
message TS6029: Version 1.7.5
$ tsc src/HOC.tsx typings/react/react.d.ts --jsx preserve --module commonjs
src/HOC.tsx(19,22): error TS2600: JSX element attributes type 'T' must be an object type.
src/HOC.tsx(46,18): error TS2600: JSX element attributes type 'MyInputProps & HighlightedProps' must be an object type.
src/HOC.tsx(47,18): error TS2600: JSX element attributes type 'MyLinkProps & HighlightedProps' must be an object type.
$ ./node_modules/.bin/tsc --version
Version 1.8.0-dev.20160106
$ ./node_modules/.bin/tsc src/HOC.tsx typings/react/react.d.ts --jsx preserve --module commonjs

@frankwallis
Copy link
Contributor Author

that's great news, thanks for the update

@mhegazy mhegazy added Fixed A PR has been merged for this issue and removed In Discussion Not yet reached consensus labels Jan 6, 2016
@mhegazy mhegazy added this to the TypeScript 1.8 milestone Jan 6, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants