I experimented with the code below in TypeScript Playground with all settings enabled. My expectation was that the TS compiler would only allow the first call()
to be valid. However, to my surprise, all four calls were accepted.
Upon inspecting the calls, I noticed they were typed as call<"String"|undefined>
. This discovery left me wondering, what exactly is happening here? Is there a way to enforce this validation?
interface IEndpoint<RequestType> { }
export const GetConsumer: IEndpoint<undefined> = {};
function call<RequestType>(rpc: IEndpoint<RequestType>, request: RequestType) {}
call(GetConsumer, undefined);
call(GetConsumer, null); // this should not be permitted
call(GetConsumer, 1); // this should not be permitted
call(GetConsumer, "String"); // this should not be permitted