Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Regression for inferred unions in generic parameters #33490

Copy link
Copy link
@eps1lon

Description

@eps1lon
Issue body actions

TypeScript Version: 3.7.0-dev.20190918

Search Terms: infer generic

Code

// simpler react types
declare class Component<P> {
  props: P;
}

export type ComponentClass<P> = {
  new (props: P): Component<P>;
};

export type FunctionComponent<P> = (props: P) => null;

export type ComponentType<P> = FunctionComponent<P> | ComponentClass<P>;

// simpler react-router.d.ts
export interface RouteComponentProps {
  route: string;
}

declare function withRouter<
  P extends RouteComponentProps,
  C extends ComponentType<P>
>(
  component: C & ComponentType<P>
): ComponentClass<Omit<P, keyof RouteComponentProps>>;

interface Props extends RouteComponentProps {
  username: string;
}

declare const MyComponent: ComponentType<Props>;

// If I define `ComponentType` as `ComponentClass<P> | FunctionComponent<P>`:
// Argument of type 'ComponentType<Props>' is not assignable to parameter of type 'ComponentClass<Props> | (ComponentClass<Props> & FunctionComponent<Props>)'.
// If I define `ComponentType` as `FunctionComponent<P> | ComponentClass<P>`:
// Argument of type 'ComponentType<Props>' is not assignable to parameter of type 'FunctionComponent<Props> | (FunctionComponent<Props> & ComponentClass<Props>)'.
withRouter(MyComponent);

C & ComponentType<P> helps inference. For a more comprehensive test suite check the react-router declarations and tests

A fix getting rid of C & ComponentType<P> (DefinitelyTyped/DefinitelyTyped#38326) caused issues when using strictFunctionTypes

Expected behavior:
No type error

Actual behavior:
Throws with a different error message depending on the order of the union members in ComponentType

Playground Link:
playground for 3.5.3

Related Issues:

/cc @OliverJAsh

Reactions are currently unavailable

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptA bug in TypeScriptDomain: check: Type InferenceRelated to type inference performed during signature resolution or `infer` type resolutionRelated to type inference performed during signature resolution or `infer` type resolutionFix AvailableA PR has been opened for this issueA PR has been opened for this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.