Skip to content

limited use of [index:string] #10

@donnut

Description

@donnut

Currently some function definitions like propEq use the index signature to type object arguments. This seems of limited use, because the supplied object needs to have this index signature explicitly defined. For example.
One of the type signatures of propEq is

propEq<T>(name: string, val: T, obj: {[index:string]: T}): boolean;

Using this function with var obj = {a: 1, b: 2} like

propEq('a', 1, obj); // Type error

is correct, however TypeScript complains with:
argument of type 'string' is not assignable to parameter of type 'number' pointing to the a parameter.
This is because obj does not have a index signature. If we add this signature to obj explicitly the type error disappears:

var obj: {[index: string]: number} = {a: 1, b: 2}
propEq('a', 1, obj); // OK

This reason for this behavior is explained in http://stackoverflow.com/questions/22077023/why-cant-i-indirectly-return-an-object-literal-to-satisfy-an-index-signature-re#22077024

I think the requirement that only objects with an index signature can be used is too strict and that we therefore should limit the use of index signature to those cases where it is obviously (but I can not think of one at the moment).

Any thoughts on this issue?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions