Skip to content

Docs: Confusions about contextual typing #1463

@icyfish

Description

@icyfish

Page URL: https://www.typescriptlang.org/docs/handbook/type-inference.html#contextual-typing

Issue:

This is the example to illustrate contextual typing in docs.
However, I don't get desired result in TS playground with this version of code.

window.onmousedown = function (mouseEvent) { // Parameter 'mouseEvent' implicitly has an 'any' type.(7006)
  console.log(mouseEvent.button);  
  console.log(mouseEvent.kangaroo);  
};

When I change the code above to version below, I get desired result like doc's.

window.addEventListener('mousedown', function(mouseEvent) {
	console.log(mouseEvent.button); //<- OK
  console.log(mouseEvent.kangaroo); //<- Error!
})

I am confused about this. Can someone gives an explanation?
Thanks so much!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions