Skip to content

Error when JSX-spreading a type to satisfy a union of types #16144

@CzBuCHi

Description

@CzBuCHi

TypeScript Version: 2.2.1

Code

export type CatInfo = { type: 'Cat'; subType: string; };
export type DogInfo = { type: 'Dog'; };
export type AnimalInfo = CatInfo | DogInfo;

function AnimalComponent(info: AnimalInfo):React.ReactElement<AnimalInfo> {
    return ...;
}

function getProps(): AnimalInfo {
    // this may be from server or whatever ...
    return { type: 'Cat', subType: 'Large' };
}

var props:AnimalInfo = getProps();
var component = <AnimalComponent {...props} />
//                               ^^^^^^^^^^
// 'Type '{ type: "Cat" | "Dog"; }' is not assignable to type '(IntrinsicAttributes & CatInfo) | (IntrinsicAttributes & DogInfo)'.
//   Type '{ type: "Cat" | "Dog"; }' is not assignable to type 'IntrinsicAttributes & DogInfo'.
//     Type '{ type: "Cat" | "Dog"; }' is not assignable to type 'DogInfo'.
//       Types of property 'type' are incompatible.
//         Type '"Cat" | "Dog"' is not assignable to type '"Dog"'.
//           Type '"Cat"' is not assignable to type '"Dog"'.'

var props2:AnimalInfo = { type: 'Cat', subType: 'Large' };
var component2 = <AnimalComponent {...props2} />
//                                ^^^^^^^^^^^
//                                No error here ...

Expected behavior:
no error or both as errors
Actual behavior:
tsc throw error for props but not for props2

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: JSX/TSXRelates to the JSX parser and emitterFixedA PR has been merged 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