microsoft / microsoft/TypeScript

Allow users to customize the variance of built-in `Array`

Open
#52,621 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

🔎 Search Terms

Array covariance hardcoded?, Array invariance, --noLib

🕗 Version & Regression Information

Tested with 4.9.5

⏯ Playground Link

https://tsplay.dev/WPzdYN

💻 Code
// @noLib: true

interface Array<T>
  { [i: number]: T | undefined
  , push: (t: T) => number
  }

interface _Array<T>
  { [i: number]: T | undefined
  , push: (t: T) => number
  }

declare let as: Array<"a">
declare let abs: Array<"a" | "b">
abs = as

declare let _as: _Array<"a">
declare let _abs: _Array<"a" | "b">
_abs = _as
🙁 Actual behavior

The assignment abs = as compiles.

🙂 Expected behavior

The assignment abs = as should not compile, just like the assignment _abs = _as does not compile.

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.

Research direction

Start by reproducing the TypeScript Playground example linked in the issue, especially the comparison between built-in Array and _Array under @noLib. Trace how the compiler handles variance for the built-in Array, then verify that abs = as is rejected like _abs = _as without changing the demonstrated custom-interface behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.