Is there a way to restrict the run call below to be strict about the type allowed by the type parameters specified to the RequestType<>? The return type R seems to work, but RQ isn't strict.
class RequestType<RQ, R> {
constructor(public readonly method: string) { }
}
interface FooRequest {
foo: string;
bar: string;
}
interface FooResponse {
foobar: string;
}
const FooRequestType = new RequestType<FooRequest, FooResponse>("foo");
function run<RQ, R>(type: RequestType<RQ, R>, request: RQ): R {
// real code here
return {} as R;
}
Here are the calls
const foo1 = run(FooRequestType, {}); // want an error here
const foo2 = run(FooRequestType, {
foo: "foo" // want an error here
});
const foo3 = run(FooRequestType, {
foo: "foo",
bar: "bar",
baz: "" // error here -- good
});
Here is a link to the TypeScript playgound. Any help is appreciated -- Thanks!