-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
By DesignDeprecated - use "Working as Intended" or "Design Limitation" insteadDeprecated - use "Working as Intended" or "Design Limitation" insteadQuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code
Description
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
Labels
By DesignDeprecated - use "Working as Intended" or "Design Limitation" insteadDeprecated - use "Working as Intended" or "Design Limitation" insteadQuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code