Skip to content

Inconsistent Treatment of Shorthand Functions #11062

Closed
@ChrisGrohHS

Description

@ChrisGrohHS

TypeScript Version: 2.0.3

Code

// A *self-contained* demonstration of the problem follows...
function testFunction(x: string | Function): any {
    if (typeof x === 'string') {
        return {
            key: function (a: any, b: any) {
                // Fine, x is string
                return a[x];
            }
        };
        return {
            key(a: any, b: any) {
                // Fails, x is string | Function here
                return a[x];
            }
        };
    }
}

Expected behavior:
Compiles fine

Actual behavior:
Error: An index expression argument must be of type 'string', 'number', 'symbol', or 'any'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions