Microsoft/TypeScript

Certain conditional types allow unsound assignments

開放

#55,733 建立於 2023年9月13日

 (6 則留言) (0 個反應) (0 位負責人)TypeScript (13,395 個分叉)batch import
BugCursed?Domain: Conditional TypesHelp Wanted

倉庫指標

星標
 (108,860 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

🔎 Search Terms

"conditional type" generic assignment

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about conditional types

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAQg9gDwDwBUB8UC8UDeUBuAhgDYCuEAXFClAL4BQ9okUAqgHYBGiAku+xABOqDNhoQEwCOwAmAZ1iIkAS3YAzIVABKGAPxsuvfkKQ6oVPKUMIIMqjQbNoHbsnRYDrvgOHoA2gDkVq62AQC6jDIQAMbEhILQalbRwMpw7FDBSugAFAj2AJRULtlojEnsKWkZEoQAtmDEECJ5hbj0UFBNwFCEHlkIeQUdvR4BwAAWygpyE3CkxDJQnNCENYKCcIIB9LRAA

💻 Code

type Box<T> = { value: T }

type UnboxInner<T> = T extends Box<infer R> ? UnboxInner<R> : { unboxed: T }
type Unbox<T> = UnboxInner<T>['unboxed']

declare function unbox<T>(x: T): Unbox<T>

function example<T>(x: T) {
  let a = unbox(x)
  a = 'this should be an error'
}

🙁 Actual behavior

In example we can assign arbitrary values to a, even though it's typed as Unbox<T>.

🙂 Expected behavior

Attempting to assign invalid values to a (just about anything, since its type depends on a type parameter) should cause a type error.

Additional information about the issue

These issues may possibly be related:

  • #52021
  • #35533

cc @webstrand

貢獻者指南