microsoft / microsoft/TypeScript

Crash: RangeError: Invalid string length in addSpans during instantiation of recursive template literal types

未關閉
#63,271 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

Bug Domain: Crashes
主要語言
Go
星號
111k
分支
14.3k
平均合併
2 天 4 小時
30 天內合併 PR
132

描述

🔎 Search Terms

RangeError: Invalid string length

addSpans crash

getTemplateLiteralType recursion

Template literal exponential growth

🕗 Version & Regression Information

Crashes in: 5.7.3, 5.8.3, 5.9.3, Nightly.

⏯ Playground Link

https://www.typescriptlang.org/zh/play/?target=7&ts=6.0.0-dev.20260319#code/C4TwDgpgBAIhDGAeAclCAPYEB2ATAzlNgK4C2ARhAE4B8UAvFAFBStSoZZ6ECsUA-FAAsUAFzs0mHAWECoAZjETO0wosEAmJSzYcp3KFsEBDbCG1tl+mQEY5ABiX2A3EyahIUAEoJiVFJJcMiQU1AA0UADKgapQ+MBUAJbYAOZ0jDqsekGEjoLR4j7wfohwSMg0EQAGACQA3pEAvgD69U1VNK7u4NAAouhgADYA9rjQjHWZUADaAApQyd6+-jwRAETGa3TGhLV1sy0A1hAgVQC64qYgro3OQA

💻 Code
type Dec<N extends number> = 
    N extends 5 ? 4 : N extends 4 ? 3 : N extends 3 ? 2 : 
    N extends 2 ? any : 
    N extends 1 ? 0 : 0;

type Recur<N extends number, S extends string> = 
    N extends 0 ? S : Recur<Dec<N>, `${S}_${S}`>;

type Explode = {
    [P in Recur<5, "a"> as `${P}_key`]: any;
};
🙁 Actual behavior
node built/local/tsc.js -noEmit  specializedSignatureIsNotSubtypeOfNonSpecializedSignature_filled_1_unvalidated_mutated_batch12.ts
D:\github\TypeScript\built\local\_tsc.js:123721
      throw e;
      ^

RangeError: Invalid string length
    at addSpans (D:\github\TypeScript\built\local\_tsc.js:62120:19)
    at getTemplateLiteralType (D:\github\TypeScript\built\local\_tsc.js:62095:10)
    at instantiateTypeWorker (D:\github\TypeScript\built\local\_tsc.js:63780:14)
    at instantiateTypeWithAlias (D:\github\TypeScript\built\local\_tsc.js:63736:20)
    at instantiateType (D:\github\TypeScript\built\local\_tsc.js:63703:29)
    at getMappedType (D:\github\TypeScript\built\local\_tsc.js:63383:67)
    at D:\github\TypeScript\built\local\_tsc.js:62756:73
    at map (D:\github\TypeScript\built\local\_tsc.js:196:19)
    at canTailRecurse (D:\github\TypeScript\built\local\_tsc.js:62756:33)
    at getConditionalType (D:\github\TypeScript\built\local\_tsc.js:62723:17)

Node.js v22.19.0
🙂 Expected behavior

The compiler should not crash.

Additional information about the issue

No response

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

使用提供的遞迴模板字面量範例或 specializedSignatureIsNotSubtypeOfNonSpecializedSignature_filled_1_unvalidated_mutated_batch12.ts 命令重現崩潰。從 built/local/tsc.js 中的 addSpans 和 getTemplateLiteralType 開始,沿著堆疊追蹤至 instantiateTypeWorker。完成的標準是編譯器回報或處理該輸入,而不會拋出 RangeError: Invalid string length。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
typescript
領域
compilers
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。