Skip to content

TSX type inference is broken when using intersection types #21632

Closed
@iyegoroff

Description

@iyegoroff

TypeScript Version: 2.7.1, 2.8.0-dev.20180204

Search Terms: jsx tsx intersection

Code

import * as React from 'react';

type Props<T> = T & {prop: string};

class Component<T = {valid: string}> extends React.Component<Props<T>> {}

<>
  <Component prop={''} invalid={''} />
  <Component prop={''} valid={''} invalid={''}/>
</>

Expected behavior:
Should generate 2 errors as in 2.6.2:

main.tsx(8,14): error TS2322: Type '{ prop: ""; invalid: ""; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Component<{ valid: string; }>> & Readonly<{ childr...'.
  Type '{ prop: ""; invalid: ""; }' is not assignable to type 'Readonly<Props<{ valid: string; }>>'.
    Property 'valid' is missing in type '{ prop: ""; invalid: ""; }'.
main.tsx(9,35): error TS2339: Property 'invalid' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Component<{ valid: string; }>> & Readonly<{ childr...'.

Actual behavior:
No errors are generated

Playground Link: -

Related Issues: maybe related #21427

Activity

RyanCavanaugh

RyanCavanaugh commented on Feb 5, 2018

@RyanCavanaugh
Member

@weswigham thoughts on this?

weswigham

weswigham commented on Feb 5, 2018

@weswigham
Member

I think the jsx inference/contextual typing PR I currently have open is mostly to fix exactly this, since I discovered the same issue when looking at changing formik.

weswigham

weswigham commented on Feb 5, 2018

@weswigham
Member

Also, hold on: those 2.6.2 errors are themselves erroneous. Setting a default doesn't constrain the type variable - literally any props should be be OK to pass for that component - we just messed up before and instantiated with the default like it was the constraint before.

RyanCavanaugh

RyanCavanaugh commented on Feb 5, 2018

@RyanCavanaugh
Member

Setting a default doesn't constraint the type variable - literally any props should be be OK to pass for that component

This was my interpretation as well. Seems useless to have a generic component if you're not allowed to infer a type from its props.

iyegoroff

iyegoroff commented on Feb 5, 2018

@iyegoroff
Author

@weswigham, so the actual behavior in 2.7.1 is correct and it will stay like this after your PR will be merged?

weswigham

weswigham commented on Feb 5, 2018

@weswigham
Member

Yeah. You should be using extends in that type definition if you actually wanted that field required.

iyegoroff

iyegoroff commented on Feb 5, 2018

@iyegoroff
Author

OK, thanks for clarification.

locked and limited conversation to collaborators on Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @weswigham@iyegoroff@RyanCavanaugh

      Issue actions

        TSX type inference is broken when using intersection types · Issue #21632 · microsoft/TypeScript