镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Missing error after spreading generic props into JSX #29509

Description

TypeScript Version: 3.2.2

Search Terms:

Code

type Component<P> = (props: P) => any;

const myHof = <ComposedComponentProps extends any>(
    ComposedComponent: Component<ComposedComponentProps>,
) => {
    type WrapperComponentProps = ComposedComponentProps & { myProp: string };
    declare const WrapperComponent: Component<WrapperComponentProps>;

    declare const props: ComposedComponentProps;

    // Expected no error, got none - good
    WrapperComponent({ ...props, myProp: '1000000' });
    // Expected error, got one - good
    WrapperComponent({ ...props, myProp: 1000000 });
}

However, when I try to do the same with React (.tsx):

import React from 'react';

const myHoc = <ComposedComponentProps extends any>(
    ComposedComponent: React.ComponentClass<ComposedComponentProps>,
) => {
    type WrapperComponentProps = ComposedComponentProps & { myProp: string };
    declare const WrapperComponent: React.ComponentClass<WrapperComponentProps>;

    declare const props: ComposedComponentProps;

    // Expected no error, got none - good
    <WrapperComponent {...props} myProp={'1000000'} />;
    // Expected error, but got none - bad!
    <WrapperComponent {...props} myProp={1000000} />;
};

Activity

  1. weswigham commented on Jan 24, 2019

    @weswigham
    Member

    Looks like the issue is caused by the extends any. As a workaround, use extends unknown (to which it should behave identically to) until we have a fix in, instead.

  2. OliverJAsh commented on Jan 24, 2019

    @OliverJAsh
    ContributorAuthor

    Aha, thanks. The reason I do the extends btw is because it's needed for arrow functions in .jsx/.tsx.

  3. weswigham commented on Jan 24, 2019

    @weswigham
    Member

    Oliver Joseph Ash (@OliverJAsh) A trailing comma on the type argument list should disambiguate as well.

  4. RyanCavanaugh commented on Jan 23, 2020

    @RyanCavanaugh
    Member

    Wesley Wigham (@weswigham) let's please merge at the start of the 3.9 branch

  5. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions