I have an interface defining an index signature:
interface MethodCollection {
[methodName: string]: (id: number, text: string) => boolean | void;
}
Everything's good here, any method I add to such an object will have their parameter types recognized correctly:
var myMethods: MethodCollection = {
methodA: (id, text) => {
// id: number
// text: string
}
}
Now, I need to add a type parameter to these functions:
interface MethodCollection {
[methodName: string]: <T>(id: number, text: string, options: T) => boolean | void;
}
The moment I do this, the TypeScript compiler barfs up:
Parameter 'text' implicitly has 'any' type
Parameter 'options' implicitly has 'any' type
Parameter 'id' implicitly has 'any' type
And indeed, neither can IntelliSense trace the correct types any longer, they all become implicit any
:
var myMethods: MethodCollection = {
methodA: (id, text, options) => {
// id: any
// text: any
// options: any
}
}
Why is this happening? How can I use generic methods with an index signature? I'm using Visual Studio 2013, my project is set to use TypeScript version 1.6.
tsc -v
into the Package Manager Console or into cmd, they both give 1.8.5. – John Weisz