Skip to content

type cast in tuples #28700

Closed
Closed

Description

@midiusRed

TypeScript Version: 3.1.6

Code

		const a1:[number, number, boolean] = [1, 0, false];
		//... code
		if (a1[2]) {
			a1[2] = false; //error TS2322
		}

Expected behavior:
no error (run in TypeScript 2.9.2)

Actual behavior:
TS2322: Type 'false' is not assignable to type 'true'.

why is the type cast to true ?

Activity

  1. AlCalzone commented on Nov 28, 2018

    @AlCalzone
    Contributor

    if (a1[2]) checks for truthiness and thus narrows the type of a[2] from boolean to true.

  2. ajafff commented on Nov 28, 2018

    @ajafff
    Contributor

    first of all, this has nothing to do with type casting. this is an issue with narrowing.

    This is fixed by #27574 which is part of v3.2.0-rc (and probably this week in v3.2.1)

  3. typescript-bot commented on Dec 12, 2018

    @typescript-bot
    Contributor

    This issue has been marked as a duplicate and has seen no activity in the last day. It has been closed automatic house-keeping purposes.

  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions