Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpyObj<T>[keyof T] not resolving to the type SpyObj members (Jasmine typings) #25181

Closed
mprobst opened this issue Jun 24, 2018 · 2 comments · Fixed by #26281
Closed

SpyObj<T>[keyof T] not resolving to the type SpyObj members (Jasmine typings) #25181

mprobst opened this issue Jun 24, 2018 · 2 comments · Fixed by #26281
Assignees
Labels
Bug A bug in TypeScript

Comments

@mprobst
Copy link
Contributor

mprobst commented Jun 24, 2018

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:

http://www.typescriptlang.org/play/#src=%0Ainterface%20Spy%20%7B%0A%20%20(...params%3A%20any%5B%5D)%3A%20any%3B%0A%0A%20%20identity%3A%20string%3B%0A%20%20and%3A%20Function%3B%0A%20%20mostRecentCall%3A%20%7Bargs%3A%20any%5B%5D%3B%7D%3B%0A%20%20argsForCall%3A%20any%5B%5D%3B%0A%7D%0A%0Atype%20SpyObj%3CT%3E%20%3D%20T%26%7B%0A%20%20%5Bk%20in%20keyof%20T%5D%3A%20Spy%3B%0A%7D%0A%0Adeclare%20function%20createSpyObj%3CT%3E(%0A%20%20%20%20name%3A%20string%2C%20names%3A%20Array%3Ckeyof%20T%3E)%3A%20SpyObj%3CT%3E%3B%0A%0A%2F**%0A%20*%20Creates%20a%20jasmine%20mock%20where%20every%20given%20method%20returns%20the%20spy%20object.%0A%20*%2F%0Afunction%20mock%3CT%3E(spyName%3A%20string%2C%20methodNames%3A%20Array%3Ckeyof%20T%3E)%3A%20SpyObj%3CT%3E%20%7B%0A%20%20const%20spy%20%3D%20createSpyObj%3CT%3E(spyName%2C%20methodNames)%3B%0A%20%20for%20(const%20methodName%20of%20methodNames)%20%7B%0A%20%20%20%20spy%5BmethodName%5D.and.returnValue(spy)%3B%0A%20%20%7D%0A%20%20return%20spy%3B%0A%7D%0A

@mprobst mprobst changed the title (Jasmine typings) SpyObj<T>[keyof T] not resolving to the type SpyObj members (Jasmine typings) Jun 24, 2018
@RyanCavanaugh
Copy link
Member

@DanielRosenwasser intended effect of 2.9's keyof changes?

@mhegazy mhegazy added the Bug A bug in TypeScript label Jun 29, 2018
@mhegazy mhegazy added this to the TypeScript 3.0 milestone Jun 29, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Jun 29, 2018

Does this work as a workaround:

type SpyObj<T> = {
    [k in keyof T]: T[k] & Spy;
}

weswigham added a commit to weswigham/TypeScript that referenced this issue Aug 8, 2018
weswigham added a commit that referenced this issue Aug 27, 2018
* unknownify accesses

* Move to simplify to break less with fewer changes

* Accept baselines for now

* Always propegate any as an index type

* Fix flow control in the presence of simplifiable types

* Add spy repro from #25181

* Retain errorType when it is used as a marker for the lack of a constraint

* Small refinement

* Add new test

* Move most potentially recursive types from isIdenticalTo into structuredTypeRelatedTo to match the non-identity relations

* Fix nits

* Doesnt need to be undefineable at all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants