Skip to content

Having noImplicitAny Not Enabled Impacts Type Inference #47294

Description

@joealden

Bug Report

🔎 Search Terms

array union types type inference noImplicitAny disabled

🕗 Version & Regression Information

This changed between versions 4.2.3 and 4.3.5, as before 4.3.5, type inference for the 2nd case inferred any regardless of if noImplicitAny was enabled. After 4.3.5, the expected inference happens when noImplicitAny is enabled, but behaves as it did before 4.3.5 when noImplicitAny is not enabled.

⏯ Playground Link

Playground link with relevant code

💻 Code

// `item` and `key` are both typed as expected (`string | number` and `number` respectively)
const array1: Array<string | number> = [];
const mapped1 = array1.map((item, key) => [item, key])

// `item` and `key` are both typed as `any`
const array2: Array<string> | Array<number> = [];
const mapped2 = array2.map((item, key) => [item, key])

🙁 Actual behavior

When noImplicitAny is not enabled in the TS config, inferred types are impacted (inferred as any in the above case).

🙂 Expected behavior

noImplicitAny shouldn't impact inferred types, or if this is expected behaviour as having this option enabled allows for better heuristics to work, then I think having it documented that this settings isn't essentially just a "lint rule" might be a good idea.

Activity

  1. DanielRosenwasser commented on Jan 6, 2022

    @DanielRosenwasser
    Member

    For anyone else checking, it has nothing to do with the initializer.

    Playground link with noImplicitAny disabled

    function foo1(fnArray1: Array<string | number>) {
        // `item` and `key` are both typed as expected (`string | number` and `number`)
        const mapped1 = fnArray1.map((item, key) => [item, key])
    }
    
    function foo2(fnArray2: Array<string> | Array<number>) {
        // `item` and `key` are both typed as `any`
        const mapped2 = fnArray2.map((item, key) => [item, key])
    }
  2. ahejlsberg commented on Jan 11, 2022

    @ahejlsberg
    Member

    This is apparently by design. See #42620. I presume code existed before that PR that depends on parameters being assigned type any when -noImplicitAny is disabled.

  3. added
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    and removed
    Needs InvestigationThis issue needs a team member to investigate its status.
    on Jan 11, 2022
  4. DanielRosenwasser commented on Jan 11, 2022

    @DanielRosenwasser
    Member

    Thanks for digging that up, totally forgot about it!

  5. typescript-bot commented on Jan 15, 2022

    @typescript-bot
    Contributor

    This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  6. 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