Microsoft/TypeScript

Generic constraint is not validated in a recursive type

Open

#53,895 建立於 2023年4月18日

在 GitHub 查看
 (1 留言) (0 反應) (0 負責人)TypeScript (6,726 fork)batch import
BugDomain: Conditional TypesHelp Wanted

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

Bug Report

🕗 Version & Regression Information

  • This is the behavior in every version I tried, starting from 4.1 where recursive conditional types were added

⏯ Playground Link

Playground link with relevant code

💻 Code

export type ComputeRangeLoose<
  N extends number,
  Result extends Array<number> = [],
> = Result['length'] extends N ? Result : ComputeRangeLoose<N, [...Result, Result['length']]>

type A = ComputeRangeLoose<{ foo: true }>

export type ComputeRangeStrict<
  N extends number,
  Result extends Array<number> = [],
> = N extends number
  ? Result['length'] extends N ? Result : ComputeRangeStrict<N, [...Result, Result['length']]>
  : never

type B = ComputeRangeStrict<{ foo: true }>

🙁 Actual behavior

ComputeRangeLoose<{ foo: true }> allows to pass a type that is not related to number, throwing Type instantiation is excessively deep and possibly infinite.

ComputeRangeStrict<{ foo: true }> throws an expected Type '{ foo: true; }' does not satisfy the constraint 'number'

🙂 Expected behavior

Both ComputeRangeLoose and ComputeRangeStrict throw Type '{ foo: true; }' does not satisfy the constraint 'number'


As the issue is reproducible since 4.1, I'm pretty sure it was already mentioned somewhere however I failed to find a suitable issue

貢獻者指南