# Typescript error - "Element implicitly has an 'any' type because type 'Object' has no index signature"

**URL:** https://forum.makecode.com/t/typescript-error-element-implicitly-has-an-any-type-because-type-object-has-no-index-signature/16019
**Category:** Help
**Created:** [October 26, 2022, 10:37pm UTC](https://forum.makecode.com/t/typescript-error-element-implicitly-has-an-any-type-because-type-object-has-no-index-signature/16019 "2022-10-26T22:37:40Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [October 26, 2022, 10:37pm UTC](https://forum.makecode.com/t/typescript-error-element-implicitly-has-an-any-type-because-type-object-has-no-index-signature/16019/1 "2022-10-26T22:37:40Z")

</div>

Anyways, I was working on a project which uses Objects - and a strange error appeared. I’ve encountered it before, but under these circumstances I wasn’t able to get around it.  
Here I have a defined object:

```auto
const CUBE_VERTICIES = {
    A: new Vertex(0, LENGTH, LENGTH), //A
    B: new Vertex(LENGTH, LENGTH, LENGTH), //B
    C: new Vertex(LENGTH, 0, LENGTH), //C
    D: new Vertex(0, 0, LENGTH), //D
    E: new Vertex(0, LENGTH, 0), //E
    F: new Vertex(LENGTH, LENGTH, 0), //F
    G: new Vertex(0, 0, 0), //G
    H: new Vertex(LENGTH, 0, 0), //H
}

```

Within a method which takes in the ‘Object’ type as an argument (the ‘vertices’ variable), I have the following setup:

```auto
let vertex
let keys = Object.keys(vertices)
for (let i = 0; i < keys.length; i++) {
    vertex = vertices[keys[i]]
}

```

After trying to compile, this line returns an error:

```auto
vertex = vertices[keys[i]]

```

```auto
Element implicitly has an 'any' type because type 'Object' has no index signature

```

Does anyone have any idea as to what is going on or a possible solution? This is pretty important, because I’ll have to swap out an object for something else if this error doesn’t get resolved, which will significantly slow my code.

---

<div class="post-metadata">

### Author: ![jwunderl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jwunderl/32/5308_2.png) [@jwunderl](https://forum.makecode.com/u/jwunderl)
#### Post date: [October 26, 2022, 10:56pm UTC](https://forum.makecode.com/t/typescript-error-element-implicitly-has-an-any-type-because-type-object-has-no-index-signature/16019/2 "2022-10-26T22:56:48Z")

</div>

if you change it to

```typescript
let vertex: Vertex
let keys = Object.keys(vertices)
for (let i = 0; i < keys.length; i++) {
    vertex = vertices[keys[i]]
}

```

does it possible work?

One thing to note is that object literals are fairly slow in static typescript, particularly on hardware and in comparison to classes – if you were to define a wrapper class like

```auto
class CubeVerticies {
    constructor(
        public a: Vertex,
        public b: Vertex,
        ...
    ) { } 
}

```

on hardware my recollection is that that will run out to something like 10-20x faster field look ups / assignments. Should note that Object.keys is one thing that is not supported on explicit classes currently ([https://makecode.com/language#semantic-differences-against-javascript](https://makecode.com/language#semantic-differences-against-javascript)) but if it’s a perf issue you will be much more likely to see gains by taking that as a first step.

---

<div class="post-metadata">

### Author: ![kwx](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/kwx/32/1545_2.png) [@kwx](https://forum.makecode.com/u/kwx)
#### Post date: [October 27, 2022, 4:09pm UTC](https://forum.makecode.com/t/typescript-error-element-implicitly-has-an-any-type-because-type-object-has-no-index-signature/16019/3 "2022-10-27T16:09:05Z")

</div>

You should also consider using a plain array instead of sequential letter keys - I haven’t compared performance, but I think it would make some logic easier, especially if you end up adding more object types.

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [October 27, 2022, 4:09pm UTC](https://forum.makecode.com/t/typescript-error-element-implicitly-has-an-any-type-because-type-object-has-no-index-signature/16019/4 "2022-10-27T16:09:21Z")

</div>

Perhaps there’s a way to define an index signature to the vertices argument?

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [October 27, 2022, 4:09pm UTC](https://forum.makecode.com/t/typescript-error-element-implicitly-has-an-any-type-because-type-object-has-no-index-signature/16019/5 "2022-10-27T16:09:25Z")

</div>

Hmm, declaring the type still throws the same error… Any other idea as to why this is going wrong?

---

<div class="post-metadata">

### Author: ![jwunderl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jwunderl/32/5308_2.png) [@jwunderl](https://forum.makecode.com/u/jwunderl)
#### Post date: [October 27, 2022, 4:56pm UTC](https://forum.makecode.com/t/typescript-error-element-implicitly-has-an-any-type-because-type-object-has-no-index-signature/16019/6 "2022-10-27T16:56:22Z")

</div>

Hm, not certain, maybe try

```typescript
vertex = vertices[keys[i]] as Vertex;

```

or even

```typescript
vertex = vertices[keys[i]] as any as Vertex;

```

? It’s a typing error so throwing enough casts at it should eventually fix it 🙂

That said @kwx s point on the data being sequential is good, if you had `CubeVertices: Vertex[]` you wouldn’t face the same issues – the problem is that indexing into an object literal loses some type information which requires it to be explicitly added back in after the fact

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [October 27, 2022, 6:15pm UTC](https://forum.makecode.com/t/typescript-error-element-implicitly-has-an-any-type-because-type-object-has-no-index-signature/16019/7 "2022-10-27T18:15:13Z")

</div>

Yes, that’s why I thought of adding a ‘label’ property into the Vertex class, so I can look it up inside of a list later. That being said, I’d have to implement something like binary search (alphabetic) to speed up the process, which I don’t really feel like doing. Although, if it comes to it it might be my last resort.

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [October 27, 2022, 6:15pm UTC](https://forum.makecode.com/t/typescript-error-element-implicitly-has-an-any-type-because-type-object-has-no-index-signature/16019/8 "2022-10-27T18:15:22Z")

</div>

Thanks a lot for the help, but the same error is still showing up 😕

---

<div class="post-metadata">

### Author: ![jwunderl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jwunderl/32/5308_2.png) [@jwunderl](https://forum.makecode.com/u/jwunderl)
#### Post date: [October 27, 2022, 6:28pm UTC](https://forum.makecode.com/t/typescript-error-element-implicitly-has-an-any-type-because-type-object-has-no-index-signature/16019/9 "2022-10-27T18:28:13Z")

</div>

Hm, sorry about that, without seeing the project I don’t have anything else I can think of immediately ☹

That said, if the “c” / “a” label is important then you can always use the fact that characters are just special numbers – e.g. index into the array with `"a".charCodeAt(0) - 97` (or `console.log("A".charCodeAt(0) - 65))` if capital) and convert from index to label with `String.fromCharCode(i + 97)` (or, again `String.fromCharCode(i + 65)` for capital letter

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [October 27, 2022, 7:35pm UTC](https://forum.makecode.com/t/typescript-error-element-implicitly-has-an-any-type-because-type-object-has-no-index-signature/16019/10 "2022-10-27T19:35:58Z")

</div>

That sounds like a great idea! I’ll try it out and see if it solves my problem with the project.

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [October 28, 2022, 5:00pm UTC](https://forum.makecode.com/t/typescript-error-element-implicitly-has-an-any-type-because-type-object-has-no-index-signature/16019/11 "2022-10-28T17:00:16Z")

</div>

That’s true, I’ll see how it performes. Although this makes it easier to organise (for me) - and hardware performance isn’t at the top of my priorites. (It would be good to make it work first 😅)  
If I may ask, what kind of projection do you use in your 3d rendering algorithms? I found an easy to implement weak projection alhorithm and I’m currently following it

---

<div class="post-metadata">

### Author: ![kwx](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/kwx/32/1545_2.png) [@kwx](https://forum.makecode.com/u/kwx)
#### Post date: [October 28, 2022, 5:39pm UTC](https://forum.makecode.com/t/typescript-error-element-implicitly-has-an-any-type-because-type-object-has-no-index-signature/16019/12 "2022-10-28T17:39:37Z")

</div>

In case it helps, I posted a very high-level overview in [[Announcement] MakeCode Arcade Mini Game Jam #4 - Outer Space Jam - #71 by kwx](https://forum.makecode.com/t/announcement-makecode-arcade-mini-game-jam-4-outer-space-jam/15689/71) . The projection is pretty much the usual perspective divide apart from using fixed-point math, see the `perspectiveTransform(vert: number[])` method in the _triangle.ts_ file. Before doing that, it uses [reentrant polygon clipping](https://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm) to ensure that it doesn’t get divide-by-zero errors or problems with behind-the-user surfaces becoming visible.

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [October 28, 2022, 5:40pm UTC](https://forum.makecode.com/t/typescript-error-element-implicitly-has-an-any-type-because-type-object-has-no-index-signature/16019/13 "2022-10-28T17:40:20Z")

</div>

_sigh_ I thought it wouldn’t have to come to this, but I’m stuck. I came to a middle ground, where I made the object into a vertex list, as such:

```auto
let CUBE_VERTICES = [
    new Vertex(LENGTH, 0, LENGTH), //A
    new Vertex(LENGTH, LENGTH, LENGTH), //B
    new Vertex(LENGTH, LENGTH, 0), //C
    new Vertex(LENGTH, 0, 0), //D
    new Vertex(0, 0, LENGTH), //E
    new Vertex(0, LENGTH, LENGTH), //F
    new Vertex(0, LENGTH, 0), //G
    new Vertex(0, 0, 0), //H
]

```

And I made the point variables (‘A’, ‘B’, ‘C’ and so on) into indexes. This adds a lot of flexiblity, and it was the solution I needed. (see below)

```auto
const A = 0
const B = 1
const C = 2
const D = 3
const E = 4
const F = 5
const G = 6
const H = 7

```

However, there was no time to celebrate, since I ran into another brick wall soon after. **MY CUBE ISN’T RENDERING** 😭 😭  
Seriously, I don’t even know is it my code or the vertices I set, but it’s not working, and that’s what matters. I didn’t want to be a leech and get help for EVERYTHING regarding this project, but I’m kind of stumped since this type of project is really new to me.

Here’s the project, break a leg

> **[3d mesh render (doesn't work)](https://arcade.makecode.com/S30076-09257-01312-40147)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

---

<div class="post-metadata">

### Author: ![kwx](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/kwx/32/1545_2.png) [@kwx](https://forum.makecode.com/u/kwx)
#### Post date: [October 28, 2022, 5:53pm UTC](https://forum.makecode.com/t/typescript-error-element-implicitly-has-an-any-type-because-type-object-has-no-index-signature/16019/14 "2022-10-28T17:53:14Z")

</div>

It is rendering just fine, it’s just in an unusual position since you’re not using the screen center as the projection center. Your offset point is at (20, 20) near the top left corner.

Here’s a quick attempt at changing that:

> **[3d mesh render (doesn't work)](https://arcade.makecode.com/S07055-91978-00732-67412)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [October 28, 2022, 6:36pm UTC](https://forum.makecode.com/t/typescript-error-element-implicitly-has-an-any-type-because-type-object-has-no-index-signature/16019/15 "2022-10-28T18:36:36Z")

</div>

Thanks a lot, I figured it was something to do with offsetting! But, the cube seems kind of stretched, and it’s supposed to be rendered facing forward (see below)

 ![pixil-frame-0](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/f/fc1d0d6d91f40857fec64748802d2ddd741f6eac.png)  
(poorly drawn, close up view of what it should roughly look like)

---

<div class="post-metadata">

### Author: ![kwx](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/kwx/32/1545_2.png) [@kwx](https://forum.makecode.com/u/kwx)
#### Post date: [October 29, 2022, 2:47am UTC](https://forum.makecode.com/t/typescript-error-element-implicitly-has-an-any-type-because-type-object-has-no-index-signature/16019/16 "2022-10-29T02:47:07Z")

</div>

The main thing is that you need to decide what your coordinate systems are, for example where is the viewer in 3D space, what’s the viewing direction, where is the cube located, and how is it oriented. Assuming that you’ll want to do more complex things such as moving the cube or viewpoint later, you’ll need to start getting that sorted out.

I’ve tweaked your code a bit to make the cube centered on (0, 0, 0) (instead of having a corner at the origin), and added a viewer position. I think this is starting to look more reasonable. If you’re not liking the perspective stretching, try tweaking the focal length. If I’m understanding it right, you’re currently working in pixel units, and your focal length of 20 is very short compared to the screen size (160x120), so you’ll get a fisheye effect for close objects.

> **[3d mesh render (doesn't work)](https://arcade.makecode.com/S07055-91978-00732-67412)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

---

<div class="post-metadata">

### Author: ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)
#### Post date: [October 29, 2022, 6:52pm UTC](https://forum.makecode.com/t/typescript-error-element-implicitly-has-an-any-type-because-type-object-has-no-index-signature/16019/17 "2022-10-29T18:52:28Z")

</div>

Wow, thanks a lot - this really helped me!
