Microsoft/TypeScript

Constraints of nested conditional types applied to constrained type variables are incorrect

Open

#59.868 aperta il 5 set 2024

Vedi su GitHub
 (0 commenti) (0 reazioni) (0 assegnatari)TypeScript (6726 fork)batch import
BugDomain: Conditional TypesHelp Wanted

Metriche repository

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

Descrizione

🔎 Search Terms

constraints conditional types type variables nested

🕗 Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.0-dev.20240904#code/C4TwDgpgBAkgzgWQIbAMYAsCWA7A5gZWACcdcB5bCAGQgDcIAbAHgBUoIAPYCbAEzihxipAHxQAvFDadufAQAMkAEgDeQkngC+8gFBQoAfijEArhD1QAXFABmSBnAgBuHTpsnsqYJgD22WwBMTPjsXDz8gsJ4IgAUHNbwyGhYeIQa5JQ09Mz4IgCUUCoWDBDAxtam0JIcTlAA9HVQAKJERD5ExaW21nYOVVA19Y0tbR2arqCQsIgoGKRppCwA7j5ZjHCsobIR6qISUlvhCspqUbjaFkbSYXJQ8qq7WgBeuvr6RpUW+j32jhY-fRcbg8Xl8-hsAGZgodbo9cLF4tMknNUmdlqs6OtgvlCp0ysAKkQzPtBg1mq12njurZfv1SY0yABrKAAIxMZTg6B8JgYvFZ0CQ-ggFLGOiAA

💻 Code

type IsMatchingStringOneLevel<T extends string> = T extends `a${string}`
  ? true
  : false;

function f2<S extends string>(x: IsMatchingStringOneLevel<S>) {
  let t: true = x; // Error
  let f: false = x; // Error
}

type IsMatchingStringTwoLevels<T extends string> = T extends `a${string}`
  ? T extends `${string}z`
    ? true
    : false
  : false;

function f3<S extends string>(x: IsMatchingStringTwoLevels<S>) {
  let t: true = x; // Error
  let f: false = x; // Ok but should be an error
}

🙁 Actual behavior

It doesn't error on the fourth assignment

🙂 Expected behavior

It should error

Additional information about the issue

This is just a variant of what was fixed for one-level conditionals in https://github.com/microsoft/TypeScript/pull/56004

Guida contributor