Microsoft/TypeScript

Couple apparent bugs with variance checking

Open

#53,210 建立於 2023年3月12日

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

倉庫指標

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

描述

Bug Report

🔎 Search Terms

variance, methods, generics

NB: I did not search very exhaustively, but I did minimize the examples, so should be pretty easy to review and close as dupe or "by design" or whatever.

🕗 Version & Regression Information

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

⏯ Playground Link

Variance checking does not interact correctly with extends:

https://www.typescriptlang.org/play?#code/C4TwDgpgBAYgPAewK7CgQQDRWagQgPigF4o4ANKCAD2AgDsATAZ3WPXwAoA3ALijICUxQrgBQogMYI6TVAEM+8OhC4QATlmWq1hEl2FQuk6bKgAjRXDpIAtmfWaV63VDnGZqNRCZIANsD4tdTYzDgAWACYhAHpoqAB3AAsEBDAmIA

Variance checking does not interact correctly with "records with methods" types:

https://www.typescriptlang.org/play?#code/C4TwDgpgBAGgPAewK7CgZQHxQLxQN4BQUUAHgBQDOAXOgJQ0B2EAbhAE4EC+BBAxggwqoAhjXhNWbLLjykaFHFgXd+g1ACMxcJAwDWDBAHcG0qML4ChUQwAsECMNSgT2OKOoB05ACwAmWgRAA

💻 Code

Variance checking does not interact correctly with extends:

type F<out A, out B> = <X extends A = A>(v: X) => B

const a: F<never, never> = v => v
const b: F<unknown, never> = a
const result: never = b(42) // whoops

Variance checking does not interact correctly with "records with methods" types:

type X<out S> = {
  x(s: S): never
}

const a: X<never> = { x: s => s }
const b: X<unknown> = a
const whoops: never = b.x(42)

🙁 Actual behavior

In both cases the typechecker allows me to inhabit never.

🙂 Expected behavior

The typechecker should reject my incorrect variance annotations (F<out A, ...> and X<out S>) and not allow me to inhabit never.


Incidentally this came to light out of some discussion in the "forall for non-functions" issue here: https://github.com/microsoft/TypeScript/issues/17574#issuecomment-1465094919, but these things don't really seem materially related.

Related to #48240.

貢獻者指南