Microsoft/TypeScript

Tail-Recursion Elimination on Conditional Types

Open

#54.082 aperta il 1 mag 2023

Vedi su GitHub
 (0 commenti) (0 reazioni) (0 assegnatari)TypeScript (6726 fork)batch import
Experience EnhancementHelp WantedSuggestion

Metriche repository

Star
 (48.455 star)
Metriche merge PR
 (Merge medio 6g 17h) (9 PR mergiate in 30 g)

Descrizione

Bug Report

🔎 Search Terms

recursion,instantiation,Tail-Recursion

🕗 Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

💻 Code

type A = { a: string, b: number }
type B = { a: number, b: string }

type UnionTheTypeOfTheSameKey<R, H> = Omit<R, keyof H> & { [k in keyof H]: k extends keyof R ? R[k] | H[k] : H[k] }

type compute<T> = { [k in keyof T]: T[k] }

type foo<T extends object[], result = {}> = T extends [infer head extends object, ...infer rest extends object[]]
    ? foo<rest, UnionTheTypeOfTheSameKey<result, head>> : result

//  expect ok type test = {a:string|number,b:string|number}
type test = compute<foo<[A, B,]>>

// When the foo length is greater than 33  The result is wrong. type test1 = {a:any,b:any}
type test1 = compute<foo<[A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B]>>

🙁 Actual behavior

// When the foo length is greater than 33 The expected is wrong. type test1 = {a:any,b:any} type test1 = compute<foo<[A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B]>>

🙂 Expected behavior

// When the foo length is Less than 999 expected test1 = {a:string|number,b:string|number} type test1 = compute<foo<[A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B]>>

Guida contributor