Closed
Description
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
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
RyanCavanaugh commentedon Feb 5, 2018
@weswigham thoughts on this?
weswigham commentedon Feb 5, 2018
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 commentedon Feb 5, 2018
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 commentedon Feb 5, 2018
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 commentedon Feb 5, 2018
@weswigham, so the actual behavior in 2.7.1 is correct and it will stay like this after your PR will be merged?
weswigham commentedon Feb 5, 2018
Yeah. You should be using
extends
in that type definition if you actually wanted that field required.iyegoroff commentedon Feb 5, 2018
OK, thanks for clarification.