Skip to content

Contextual typing doesn't work on method declaration parameters in object literals #4773

Closed

Description

IntelliSense does not assume argument type in method1 and method2 here.

interface Foo {
    method1(arg: number): void;
    method2(arg: string): void;
}

function getFoo(): Foo {
    return {
        method1(arg) {
            // arg is any
        },
        method2(arg) {
            // arg is any
        }
    }
}

function getFoo2(): Foo {
    return {
        method1: (arg) => {
            // arg is number
        },
        method2: (arg) => {
            // arg is string
        }
    }
}

function getFoo3(): Foo {
    return {
        method1: function (arg) {
            // arg is number
        },
        method2: function (arg) {
            // arg is string
        }
    }
}

Activity

  1. added
    Bug
    on Sep 14, 2015
  2. changed the title [-]Type assumption is broken in ES6 object literal method shorthand[/-] [+]Contextual typing doesn't work on method declarations in object literals[/+] on Sep 17, 2015
  3. DanielRosenwasser commented on Sep 17, 2015

    @DanielRosenwasser
    Member

    According to the tests I've written so far, this appears to be affected in the language service, not the compiler itself.

    This is a contextual typing bug.

  4. changed the title [-]Contextual typing doesn't work on method declarations in object literals[/-] [+]Contextual typing doesn't work on method declaration parameters in object literals[/+] on Sep 18, 2015
  5. added
    Fixed
    on Sep 18, 2015
  6. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugFixed

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions