0

I am trying to convert the following to work, but I think it is an Es5/ES6 issue. Help Appreciated.

this.rows = Array.from(Array(Math.ceil(this.subCategoryModels.length / 3)).keys());

I currently get typscript errors:

[ts] 
Property 'from' does not exist on type 'ArrayConstructor'.
any

and

[ts] 
Property 'keys' does not exist on type 'any[]'.
any
5
  • 2
    maybe github.com/Microsoft/TypeScript/issues/8174 is relevant Commented Aug 1, 2016 at 21:47
  • 1
    "I think it is an Es5/ES6 issue" - Are you trying to write for ES5 or ES6? Both Array.from() and .keys() are ES6 methods, so obviously they won't work in ES5. Commented Aug 1, 2016 at 21:47
  • 1
    try to insert 'new' like: "Array.from( new Array(Math..." and see if that helps. Commented Aug 1, 2016 at 21:48
  • I try the following, but I still get the same errors. this.rows = Array.from(new Array(Math.ceil(this.subCategoryModels.length / 3)).keys()); Commented Aug 1, 2016 at 21:54
  • This works, but I don't think it is as elegant. for (var index = 0; index < Math.ceil(this.subCategoryModels.length / 3); index++) { this.rows.push(index); } Commented Aug 1, 2016 at 21:55

3 Answers 3

1

Property 'from' does not exist on type 'ArrayConstructor'.

This points to the fact that you want to use ES6 type definitions while still compiling to ES5.

Recommend the lib option in TypeScript latest:

"compilerOptions": {
    "target": "es5",
    "lib": ["es6", "dom"]
}

More

https://basarat.gitbooks.io/typescript/content/docs/types/lib.d.ts.html#lib-option

Sign up to request clarification or add additional context in comments.

Comments

0

Since you're seeing these errors, I'm assuming you're compiling to ES5. You can use typings to make Typescript recognize the es2015 functions for arrays.

First, install typings if not already installed.

npm install typings --global

Once you've installed typings, install type type definitions for ES2015 array functions.

typings install -SG env~es2015-array

If you're targeting a browser that does not support the new Array functions, you'll also need to add a polyfill.

Comments

0

I ended up doing the following:

html

  <ion-row *ngFor="let trio of getTriples()">
    <ion-col *ngFor="let item of trio" (click)="itemTapped(item)">
      <div class="row responsive-md">
        <div id="icon-image-{{item.id}}"><img src="data:image/png;base64,{{item.icon}}" height="75" width="75" /></div>
        <p>{{item.name}}</p>
      </div>
    </ion-col>
  </ion-row>

typescript

  getTriples() {
    let triples = [];
    let length = this.subCategoryModels.length;
    for (let i = 0; i < length; i += 3) {
      let trio = [];
      trio.push(this.subCategoryModels[i]);
      if (i + 1 < length) {
        trio.push(this.subCategoryModels[i + 1]);
      }
      if (i + 2 < length) {
        trio.push(this.subCategoryModels[i + 2]);
      }
      triples.push(trio);
    }
    return triples;
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.