Skip to content

Commit

Permalink
Add strong return type to 'invoke' command (cypress-io#4022)
Browse files Browse the repository at this point in the history
  • Loading branch information
mayfieldiv committed Aug 2, 2019
1 parent 7e2ca83 commit 0bac696
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
6 changes: 1 addition & 5 deletions cli/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -811,14 +811,10 @@ declare namespace Cypress {

/**
* Invoke a function on the previously yielded subject.
* This isn't possible to strongly type without generic override yet.
* If called on an object you can do this instead: `.then(s => s.show())`.
* If called on an array you can do this instead: `.each(s => s.show())`.
* From there the subject will be properly typed.
*
* @see https://on.cypress.io/invoke
*/
invoke(functionName: keyof Subject, ...args: any[]): Chainable<Subject> // don't have a way to express return types yet
invoke<K extends { [K in keyof Subject]: Subject[K] extends Function ? K : never }[keyof Subject], R extends Subject[K] extends (...args: any[]) => infer R ? R : never>(functionName: K, ...args: any[]): Chainable<R>;

/**
* Get a property’s value on the previously yielded subject.
Expand Down
3 changes: 3 additions & 0 deletions cli/types/tests/chainer-examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ cy.wrap('foobar').should('not.have.string', 'baz')

cy.wrap('foobar').should('not.include', 'baz')

cy.wrap({ foo: (a:string) => 1, bar: 2 }).invoke('foo').should('equal', 1)
// cy.wrap({ foo: () => 1, bar: 2 }).invoke('bar') // compile ERROR

;
() => {
let val = 1
Expand Down

0 comments on commit 0bac696

Please sign in to comment.