Skip to content

No string literal type for optional property in JSX #21028

@ghost

Description

TypeScript Version: 2.7.0-dev.20180104

Code

import * as React from "react";

interface Props {
    x?: "a" | "b";
}
class MyComponent<P extends Props = Props> extends React.Component<P> {}
const m = <MyComponent x="a"/>

Expected behavior:

No error.

Actual behavior:

src/a.tsx(7,24): error TS2322: Type '{ x: string; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<MyComponent<Props>> & Readonly<{ children?: ReactN...'.
  Type '{ x: string; }' is not assignable to type 'Readonly<Props>'.
    Types of property 'x' are incompatible.
      Type 'string' is not assignable to type '"a" | "b" | undefined'.

This was working in 2.7.0-dev.20180103.
There is no error if x is made non-optional.
But discovered in react-leaflet on DefinitelyTyped. There are also errors in react-router that look similar.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions