Skip to content

TSX data-* props issue with Stricter Assignability Checks to Unions with Index Signatures #36935

@jraoult

Description

@jraoult

Seems like the new Stricter Assignability Checks to Unions with Index Signatures in 3.8 makes it impossible to have data-* props passed to React components when their props type is based on a union.

Pardon the contrived example below (I tried to reduce as much as possible for clarity sake). For a real case most material UI components props types are using unions and therefor fail the same test.

TypeScript Version: 3.8-Beta

Search Terms:
data
Stricter Assignability Checks to Unions with Index Signatures

Expected behavior:
The data- props should be accepted all the time (3.7 behaviour)

Actual behavior:
With 3.8 any extra data-* will be rejected if the props type is based on a union.

Related Issues:
In another repo, but may be this mui/material-ui#19536 ?

Code

import React, { ReactElement} from "react";

declare function NotHappy(props: ({ fixed?: boolean } | { value?: number })): ReactElement;
declare function Happy(props: { fixed?: boolean, value?: number }): ReactElement;

const RootNotHappy = () => (<NotHappy data-testid="my-test-id" />)
const RootHappy = () => (<Happy data-testid="my-test-id" />)
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "Latest",
    "jsx": "Preserve",
    "module": "ESNext"
  }
}

Playground Link: Provided

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions