Closed
Description
TypeScript Version: nightly (2.2.0-dev.20161203)
I currently find it somewhat hard to precisely describe the issue/suggestion, but I believe this use case should demonstrate it clearly:
type MethodDescriptor = {
name: string;
args: any[];
returnValue: any;
}
function dispatchMethod<M extends MethodDescriptor>(name: M['name'], args: M['args']): M['returnValue'];
type SomeMethodDescriptor = {
name: "someMethod";
args: [number, string];
returnValue: string[];
}
// Currently, there's no compilation error on the the invalid second argument type and the
// return type is inferred as "any" instead of "string[]":
let result = dispatchMethod<SomeMethodDescriptor>("someMethod", ["hello", 35]);
The general idea here is that the types are used unconventionally: not to describe interfaces to run-time objects but as a way to "pack" a set of type arguments into an "object-like" compile-time entity. Perhaps this could be called something like "packed type arguments" or "type argument classes" or maybe "type argument schema containers"? I'm not sure.
Anyway, I was trying to write a general purpose dispatcher that can invoke a set of actions, and that seemed like a more elegant and type safe alternative to the more conventional:
function dispatchMethod<A extends any[], R>(name: string, args: A): R;
dispatchMethod<[number, string], string[]>("someMethod", ["hello", 35]); // <- errors as expected