microsoft / microsoft/TypeScript

Variance annotations are not allowed in type aliases for intersections

Open
#59,561 5 comments 2 reactions 1 assignee View on GitHub

@ahejlsberg is already working on this.

Since Aug 9, 2024.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

variance measurement markers sub sup intersection type aliases

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.6.0-dev.20240808#code/C4TwDgpgBAKgYgewQHgGoEMA2BXCA+KAXigG8oFtgAuKDHaAXwG4AoUSWAIXQCc0tcBYmQCWAOxoAKXgHMadXAEoiBAG4IRAEyjM24aDG4AvZOPKVaA-EViIUC6wDIuvfvTysgA

💻 Code
interface Foo<Value> {
  out: Value;
}
interface Bar<Value> {
  in: (arg: Value) => void;
}

// }
interface Foo<Value> {
  out: Value;
}
interface Bar<Value> {
  in: (arg: Value) => void;
}

type Baz<in out Value> = Foo<Value> & Bar<Value>;

type First<in out Value> = Foo<Value>;
type Second<in out Value> = Bar<Value>;

// Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.(2637)
type Baz<in out Value> = Foo<Value> & Bar<Value>;

// both members are allowed to have annotations
type First<in out Value> = Foo<Value>;
type Second<in out Value> = Bar<Value>;
🙁 Actual behavior

Error is raised

🙂 Expected behavior

I'd expect this to work, especially since https://github.com/microsoft/TypeScript/pull/56418 allowed it to work on more types. Note that this issue isn't even strictly related to using generic references as constituent members. The same issue happens with type aliases and interfaces

Additional information about the issue

Maybe this is expected but I couldn't quickly find any issue related to it or an explicit mention of intersections being purposefully disallowed here

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.