Closed
Description
TypeScript Version: 2.9.2
Code
interface Spy {
(...params: any[]): any;
identity: string;
and: Function;
mostRecentCall: {args: any[];};
argsForCall: any[];
}
type SpyObj<T> = T&{
[k in keyof T]: Spy;
}
declare function createSpyObj<T>(
name: string, names: Array<keyof T>): SpyObj<T>;
/**
* Creates a jasmine mock where every given method returns the spy object.
*/
function mock<T>(spyName: string, methodNames: Array<keyof T>): SpyObj<T> {
const spyObj = createSpyObj<T>(spyName, methodNames);
for (const methodName of methodNames) {
spyObj[methodName].and.returnValue(1); // fails, Property 'and' does not exist on type 'SpyObj<T>[keyof T]'.
}
return spyObj;
}
Expected behavior:
In TS2.8, spyObj[methodName]
above was any, allowing and
to be referenced on it. Ideally, this would figure out that all members of spyObj
must at least be a Spy
.
Actual behavior:
In TS 2.9, there's a type SpyObj<T>[keyof T]
, but apparently it does not resolve to Spy
?
Playground Link: