Skip to content

Try enforcing contextual type of array literal elements #19541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
RyanCavanaugh opened this issue Oct 27, 2017 · 2 comments
Open

Try enforcing contextual type of array literal elements #19541

RyanCavanaugh opened this issue Oct 27, 2017 · 2 comments
Labels
Breaking Change Would introduce errors in existing code Committed The team has roadmapped this issue Suggestion An idea for TypeScript
Milestone

Comments

@RyanCavanaugh
Copy link
Member

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

@RyanCavanaugh RyanCavanaugh added Breaking Change Would introduce errors in existing code Committed The team has roadmapped this issue Suggestion An idea for TypeScript labels Oct 27, 2017
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 2.7 milestone Oct 27, 2017
@RyanCavanaugh RyanCavanaugh self-assigned this Oct 27, 2017
@ahejlsberg
Copy link
Member

@RyanCavanaugh On thing that would trip this up is overload resolution where we check the same expression multiple times with different contextual types.

@DanielRosenwasser
Copy link
Member

Half of this is fixed. Thanks @weswigham.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking Change Would introduce errors in existing code Committed The team has roadmapped this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants