Microsoft/TypeScript

False positive ts(2373)

Open

#58,794 创建于 2024年6月7日

在 GitHub 查看
 (0 评论) (0 反应) (0 负责人)TypeScript (6,726 fork)batch import
Domain: BinderHelp WantedPossible Improvement

仓库指标

Star
 (48,455 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 9 个 PR)

描述

🔎 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

https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&noUnusedLocals=true&noUnusedParameters=true&esModuleInterop=false&declaration=false&target=99&jsx=0&noErrorTruncation=true&pretty=true&isolatedDeclarations=true&verbatimModuleSyntax=true&useUnknownInCatchVariables=true&exactOptionalPropertyTypes=true&noFallthroughCasesInSwitch=true&noImplicitOverride=true&noPropertyAccessFromIndexSignature=true&ts=5.6.0-dev.20240606&ssl=18&ssc=1&pln=1&pc=1#code/PQKhCgAIUgVALAlgZ0gMwK4DsDGAXRAey0j3gCdCB3VAAwCUBTNR8x3RgUXMvIC5IAYQCGWLITyRhOHI2SoA5ABt2AczILIAI2aE2kRFkQFhSxAC9hBYrSmTy2AgFtGAOigRocAMqQcetnwlAE9INgAHPTw6aIAKACYAZgB2RIBKWldIQF4NwFkdj2BwRgAPSPJJfyxkSTwHRgAFQmRjRAA3RkgAXkgAHkrquAA+WIBvSABtFSx1eEgAWkgARgBdATRDUwBJPEYnABpIKZnIAF8BNmEAE2IQuHHltIFYSAAfSGxL5kNGS67ByBGUDCjDwGHIJHWWC2OycAG5wCd4eBQJ4YAgUOhsPgiCQqHoANaoSEdKxhRyIFzuaAeHzQECGMD+HiMIKhCJRGLIBIpdKZHKAOR2CkVSlE-MQBmhTMgGk0Wu0ur1+pJYMMxkcyAdJmoyPMlqt0BslNtdqdzowrjdQrB7o84K93lhPsTfp1-oDIMDQeCDVCjTD4YjwEA

💻 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.

贡献者指南