Skip to content

Incorrect type inferrence for default-initialized parameters and noImplicitAny #36052

Closed

Description

@dave-hart

Having a default initialized parameter in a function declaration passed to a function incorrectly asserts that the parameter is an any type

TypeScript Version:
3.7.4 (also reproduced on Nightly)
I recently upgraded from Typescript 3.5.2 to 3.7.3. The code below was fine with ts 3.5.2

Search Terms:
default parameter noImplicitAny
Code

function execute(script: string | Function): Promise<string> {
  return new Promise((resolve, reject) => {
    if (typeof script === 'string') {
      reject('rejecting promise');
    }
    resolve('resolving promise');
  });
}

export function executeSomething() {
  return execute((root: HTMLElement, debug = true) => {
    if (debug) {
      root.innerHTML = '';
    }
  });
}

Expected behavior:
No Errors

Actual behavior:
I get an error for the debug = true statement. "TS7006: Parameter 'debug' implicitly has an 'any' type."

Playground Link:
Playground link

Related Issues:
None

Activity

  1. RyanCavanaugh commented on Jan 7, 2020

    @RyanCavanaugh
    Member
    // No error
    const f1 = (debug = true) => false;
    // Error, but shouldn't be -- contextual type from 'Function'
    // shouldn't have negative effects here
    const f2: Function = (debug = true) => false;
  2. ahejlsberg commented on Jan 24, 2020

    @ahejlsberg
    Member

    This is actually working as intended. For f1 it is meaningful to infer from the initializer because the inferred type will constrain arguments that can be passed for debug. But for f2 we've already established that the type is Function. Thus, anything we infer for debug will be disregarded, and we have no idea what type of arguments will actually be passed (and keep in mind that the default value only kicks in when we're passed an undefined).

    It's a similar discussion to here #30840 (comment).

  3. added
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    and removed
    BugA bug in TypeScript
    on Jan 24, 2020
  4. typescript-bot commented on Jan 26, 2020

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

  5. ahejlsberg commented on Jan 28, 2020

    @ahejlsberg
    Member

    Having thought about it some more, I now agree that this is a bug. #36476 has the fix.

  6. added
    BugA bug in TypeScript
    and removed
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    on Jan 28, 2020
  7. 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

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions