Skip to content

Instantly share code, notes, and snippets.

@Quramy
Created February 5, 2018 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Quramy/39df01a82ae2eb2b6092dbb85662017f to your computer and use it in GitHub Desktop.
Save Quramy/39df01a82ae2eb2b6092dbb85662017f to your computer and use it in GitHub Desktop.
Value mocking example using type inference in conditional types
type RT<F> = F extends (...args: any[]) => infer R ? R : never;
interface Proxy<T, K extends keyof T> {
returnValue(value: RT<T[K]>): this;
}
declare function spyOn<T, K extends keyof T>(obj: T, methodName: K): {
and: Proxy<T, K>;
};
const serviceToTest = {
methodToBeMocked() {
return "SomeStringHardToFetch";
}
};
spyOn(serviceToTest, "methodToBeMocked").and.returnValue("fake value");
// spyOn(serviceToTest, "methodToBeMocked").and.returnValue(10000); // Compilation Error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment