microsoft / microsoft/TypeScript
False positive ts(2373)
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
Parameter cannot reference identifier declared after it, ts(2373)
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about anything related (and found nothing).
⏯ Playground Link
💻 Code
/**
* This function throws `ReferenceError: Cannot access 'length' before initialization` at runtime.
*
* TS correctly reports `ts(2373)`. 👍
*/
export const truePositive = <const T>({ [length - 1]: finalItem, length }: readonly T[]): T | undefined => {
return finalItem;
};
/**
* This function works fine at runtime.
*
* TS **in**correctly reports `ts(2373)`. 👎
*/
export const falsePositive = <const T>({ length, [length - 1]: finalItem }: readonly T[]): T | undefined => {
return finalItem;
};
🙁 Actual behavior
A false positive ts(2373) is being reported in a situation where we can determine statically that what the report warns about is not taking place.
Ironically, the false positive ts(2373) might represent a real bug in TS' own modeling of declaration order.
🙂 Expected behavior
No false positive ts(2373) being reported.
Additional information about the issue
To be clear:
In the falsePositive case, we can statically determine that length is declared prior to being used.
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 linked TypeScript Playground reproduction and compare the truePositive and falsePositive parameter patterns. Trace the implementation of diagnostic ts(2373), then add a regression test for the shown declaration-order case. Done means the falsePositive example produces no diagnostic while truePositive still reports the runtime hazard.
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
- Mostly clear
- Newbie friendliness
- 35/100