microsoft / microsoft/TypeScript
Error messaging improvements when accidentally missing an = in JSX attributes
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Bug Report
I think we could give better errors when you miss an = between JSX attributes. Coming from this tweet.
🔎 Search Terms
JSX spread expected attribute
🕗 Version & Regression Information
N/A - Tested in nightly
⏯ Playground Link
import React from "react"
const shouldDisable = true
const b = () => {
<div aria-disabled {shouldDisable} />
// ^^^^^^^^^^^^^ error: '...' expected.(1005)
}
The gist is that TypeScript thinks you will always be using {thing} as a spread {...thing} - but it's also just possible that you missed the =. In this case specifically, shouldDisable is a boolean, which can never spread.
Two ways we could safely improve:
Checking the type of the value in the { }:
// In this case `aria-disabled` is a boolean, and so including it here defaults to 'true' (and doesn't error)
// shouldDisable is a boolean which can never spread like current error: '...' expected.(1005)
// TS could check if token before is a jsx identifier and then recommend to add the `=`
// error: "'shouldDisable' cannot be spread, did you mean to write 'aria-disabled={shoudlDisable}"?
const shouldDisable = true
const b = () => {
<div aria-disabled {shouldDisable} />
}
Checking whether the identifier matches an attribute:
import React from "react"
const onFocus = () => {}
// onFocus known to be function, so this 2nd error: '...' expected.(1005)
// TS could first look to see if there's an attribute with the same name and recommend:
// error: "'onFocus' is not being used to spread inside the JSX element, did you mean to write 'onFocus={onFocus}"?
const a = () => {
<div onFocus {onFocus} />
}
🙁 Actual behavior
It's always an error and assumed to be a spread but without the spread
🙂 Expected behavior
It'd still be an error, but it could be an error which provides the next step.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided TypeScript JSX playground and Workbench reproduction for a missing = between JSX attributes, then inspect how the current '...' expected diagnostic is produced. Done means the same mistake still errors but provides a useful suggestion to add the missing = without misrepresenting the expression as a spread.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react, typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100