Skip to content

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

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

Closed
dave-hart opened this issue Jan 7, 2020 · 4 comments · Fixed by #36476
Closed

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

dave-hart opened this issue Jan 7, 2020 · 4 comments · Fixed by #36476
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@dave-hart
Copy link

dave-hart commented Jan 7, 2020

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

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jan 7, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.8.1 milestone Jan 7, 2020
@RyanCavanaugh
Copy link
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;

@ahejlsberg
Copy link
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).

@ahejlsberg ahejlsberg added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Bug A bug in TypeScript labels Jan 24, 2020
@ahejlsberg ahejlsberg removed this from the TypeScript 3.8.1 milestone Jan 24, 2020
@typescript-bot
Copy link
Collaborator

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

@ahejlsberg
Copy link
Member

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

@ahejlsberg ahejlsberg added Bug A bug in TypeScript and removed Working as Intended The behavior described is the intended behavior; this is not a bug labels Jan 28, 2020
@ahejlsberg ahejlsberg assigned ahejlsberg and unassigned elibarzilay Jan 28, 2020
@ahejlsberg ahejlsberg added the Fix Available A PR has been opened for this issue label Jan 28, 2020
@ahejlsberg ahejlsberg added this to the TypeScript 3.8.1 milestone Jan 28, 2020
@ahejlsberg ahejlsberg reopened this Jan 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants