Skip to content

TSC incorrectly selects overloaded method signature #327

@chebum

Description

@chebum

Hi,

I added a strongly typed jQuery.each method overload in order to perform type check when compiling my ts files:

each<T>(collection: { [key: string]: T }, callback: (indexInArray: string, valueOfElement: T) => any): any;   // Added by me
each(collection: any, callback: (indexInArray: any, valueOfElement: any) => any): any;

When using this method, tsc doesn't perform type check and allows you to write a code that will fail at runtime. For example:

/// <reference path="../../Lib/jquery.d.ts" />
var args: { [key: string]: number } = {};
$.each(args, (index: number, val) => {
    var chr = val.charAt(0);
});

There is no error at compile time, because val is treated as any. This happens because tsc chooses less specific overload with anys. However, I'd like to see a compilation error.

I do see compilation error when I write it as:

$.each(args, (index, val) => {
    var chr = val.charAt(0);
});

Why is that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" insteadQuestionAn 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