Skip to content

Functions with overloads don't automatically get types for their parameters #32037

Closed
@fregante

Description

@fregante

TypeScript Version: 3.5.2

Search Terms:

noImplicitAny true
overloaded
inferred types

Code

I don't know the exact terms for what's happening in the first example. Are these parameters' "types being inferred"?

// `text` has type `string`
window.alert = (text) => {
	return window.alert(text);
}

// Both parameters are `any`, because `addEventListener` has overloads
window.addEventListener = (type, fn) => {
	return window.addEventListener(type, fn);
}

Expected behavior:
They should follow these types from lib.dom.d.ts:

addEventListener<K extends keyof WindowEventMap>(type: K, listener: (this: Window, ev: WindowEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;

Actual behavior:

Parameter 'type' implicitly has an 'any' type. ts(7006)
Parameter 'fn' implicitly has an 'any' type. ts(7006)

Playground Link: https://www.typescriptlang.org/play/#src=window.alert%20%3D%20(text)%20%3D%3E%20%7B%0D%0A%09return%20window.alert(text)%3B%0D%0A%7D%0D%0A%0D%0Awindow.addEventListener%20%3D%20(type%2C%20fn)%20%3D%3E%20%7B%0D%0A%09return%20window.addEventListener(type%2C%20fn)%3B%0D%0A%7D

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions