microsoft / microsoft/TypeScript

Type error not indicated when spreading into a JSX component whose props type is defined as interface

Open
#43,760 11 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: JSX/TSX
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Bug Report

🔎 Search Terms

react, component props, interface type, spread

🕗 Version & Regression Information

Bug present in 3.3.3 (oldest version available on Playground), as well as Nightly.

⏯ Playground Link

Playground link with relevant code

💻 Code
import * as React from 'react'

// Change interface to a type declaration and the bug disappears.
interface Props {}
// type Props = {}

// Change 'Props' => '{}' (ie. inline the type) and the bug disappears.
function Component(_: Props) { 
    return <div>C2</div>
}

export function Bug() {
    const props = { opt: "1" } as const

    // Should be a type error, since Component doesn't accept 'opt' prop
    return <Component {...props} />
}
🙁 Actual behavior

No error is indicated.

Error is indicated correctly when interface is changed to a type declaration, or the type is inlined.

🙂 Expected behavior

TypeScript should indicate a type error since Component doesn't accept the 'opt' prop.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground reproduction and compare JSX spread checking when Props is an interface, a type alias, or an inline type. Trace the type-checking path for the Component call and verify the fix by restoring the expected error for the extra opt prop while preserving the other cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.