Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Erronous then function signature definition ElementArrayFinder #3696

Closed
aktraore opened this issue Nov 3, 2016 · 3 comments
Closed

Erronous then function signature definition ElementArrayFinder #3696

aktraore opened this issue Nov 3, 2016 · 3 comments
Assignees
Milestone

Comments

@aktraore
Copy link

aktraore commented Nov 3, 2016

When trying to use the .then function available for ElementArrayFinder by passing a callback in which I don't return anything, the typescript compiler signals it as an error, and the output is

type (value: any) => void is not assignable to parameter of type '(value: any= => {} | IThenable <{}>'

below a test function taken from the documentation that will replicate the problem.

  static test(): void {
    element.all(by.css('.items li')).then( (arr) => {
      console.log('test');
    });
  }

For me the problem comes from the type definition in lib/element.ts line 482 and 483 instead of

then(fn?: (value: any) => {} | wdpromise.IThenable<{}>, errorFn?: (error: any) => any):
      wdpromise.Promise<any[]>

it should be

then(fn?: (value: any) => void | {} | wdpromise.IThenable<{}>, errorFn?: (error: any) => any):
      wdpromise.Promise<any>

since what is returned by the callback passed in the .then may or may not be an array.

As a suggestion maybe the .then typing definition should be inspired by the typing definition of ES2015 by the TypeScript team.

  • TypeScript Version: 2.0.3
  • Node Version: 6.9.1
  • Protractor Version: 4.0.10
  • Angular Version: 1.5.0
@aktraore
Copy link
Author

aktraore commented Nov 14, 2016

It's nice that you have corrected a part of the problem with your commit @mgiambalvo but as I specified earlier the return type of element.then is wrong, putting wdpromise.Promise<any[]> forces the output to be an array, that causes a typescript error if you return anything in the .then that is different from any[]. for example
if I use this example
static test(): Promise < void >{ return element.all(by.css('.items li')).then( (arr) => { console.log('test'); }); }
the typescript compiler will throw an error saying that Promise<any[]> is not assignable to Promise<void> you cannot force the return type of the element.then() to be an array.

heathkit added a commit to heathkit/protractor that referenced this issue Nov 14, 2016
Fixes angular#3696. Also clean up the restart spec test.
heathkit added a commit that referenced this issue Nov 15, 2016
Fixes #3696. Also clean up the restart spec test.
@laurence-myers
Copy link

I'm curious, the documentation for ElementArrayFinder says it returns an array of ElementFinders. Why do we have to use any and not ElementFinder | ElementFinder[] as the return type?

jsDoc for ElementArrayFinder.then():

@returns {!webdriver.promise.Promise} A promise which will resolve to
an array of ElementFinders represented by the ElementArrayFinder.

@aktraore
Copy link
Author

aktraore commented Nov 26, 2016

@laurence-myers I hope this is can help you understand the issue, In this example, if we use ElementFinder | ElementFInder[] as the return type, the typescript compiler will say that Promise<ElementFinder | ElementFinder[]> is not assignable to Promise<void>

static test(): Promise <void>{ 
      return element.all(by.css('.items li')).then( 
             (arr) => { console.log('test'); }
      ); 
}

but if we use any, it won't throw an error since Promise<any> is assignable to Promise<void>. There is no way to predetermine what treatment will be done in the callbacks so the best you can do is type .then() as a function that returns a promise of any, any other typing would cause errors. I don't know if what I just wrote made any sense, I hope it helps.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants