Skip to content

Array type gets inferred incorrectly #53380

Closed as not planned
Closed as not planned

Description

@tippfelher

Playground

const var1: string = "";
const var2: number = 0;

// inferred as: const arr: (string | number)[]
//   this is wrong (?) and should be: const arr: [string, number]
const arr = [var1, var2];

const arr3: (typeof arr) = ["not", "ok"]; // not ok

const bettarr: [string, number] = [var1, var2];

const bettarr3: (typeof bettarr) = ["not", "ok"]; // ok

Activity

  1. fatcerberus commented on Mar 20, 2023

    @fatcerberus

    This is working as intended. To infer tuples from an array literal without a type annotation, you have to use as const.

    const var1: string = "";
    const var2: number = 0;
    
    // inferred as: const arr: readonly [string, number]
    const arr = [var1, var2] as const;
    
    const arr3: (typeof arr) = ["not", "ok"]; // ok, now an error

    Next time please follow the issue template for bug reports.

  2. locked as resolved and limited conversation to collaborators on Oct 22, 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

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions