microsoft / microsoft/TypeScript

Try enforcing contextual type of array literal elements

Open
#19,541 2 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Breaking Change Committed Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Part of #19236

Code

const s1: string[] = ["x", 3, null as any];
const s2: string[] = ["x", 3];

Expected behavior:

  • s1 should be an error
  • s2 should have an error span on 3

Actual behavior:

  • s1 is not an error
  • s2 has an error span on s2, which makes it impossible to figure out which element is wrong

Hypothesis: When checking a contextually-typed array literal, if an element type isn't assignable to the contextual type, issue an error on that element and return unknownType

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

Reproduce the two array-literal examples from the issue and inspect the compiler's contextual checking for array literal elements. Done means the first assignment is rejected and the second reports the error on the invalid element 3 rather than on s2.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.