Created
February 5, 2018 07:10
-
-
Save Quramy/39df01a82ae2eb2b6092dbb85662017f to your computer and use it in GitHub Desktop.
Value mocking example using type inference in conditional types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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