Typescript cannot define parameter types for common methods in indexes

I have an interface defining an index signature:

interface MethodCollection {
    [methodName: string]: (id: number, text: string) => boolean | void;
}

Everything is fine here, any method that I add to such an object will be correctly recognized by their parameter types:

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 is shutting down:

The text parameter is implicitly of type any

The parameters parameter is implicitly of type any

The id parameter is implicitly of type any

Indeed, IntelliSense can no longer track the correct types, they become implicit any:

var myMethods: MethodCollection = {
    methodA: (id, text, options) => {
        // id: any
        // text: any
        // options: any
    }
}

? ? Visual Studio 2013, TypeScript 1.6.

+4
1

, .

interface MethodCollection<T> {
    [methodName: string]: (id: number, text: string, options: T) => boolean | void;
}

var myMethods: MethodCollection<string> = {
    methodA: function(id, text, options) {
        // id :number,
        // text: string,
        // options: string
    }
}

VSCode tsc 1.9.0 ( typescript @next) .

.

MethodCollection.

var myMethods: MethodCollection<string | boolean> = {
    methodA: function(id, text, options) { 
        // options is string | boolean
    }
}
+2

All Articles