# Code Works in Javascript But not Blocks

**URL:** https://forum.makecode.com/t/code-works-in-javascript-but-not-blocks/21782
**Category:** Help
**Created:** [August 11, 2023, 4:41pm UTC](https://forum.makecode.com/t/code-works-in-javascript-but-not-blocks/21782 "2023-08-11T16:41:29Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Unique](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unique/32/32724_2.png) [@Unique](https://forum.makecode.com/u/Unique)
#### Post date: [August 11, 2023, 4:41pm UTC](https://forum.makecode.com/t/code-works-in-javascript-but-not-blocks/21782/1 "2023-08-11T16:41:29Z")

</div>

So to summarize the issue at hand, I created an essential custom block extension for an upcoming game. And the blocks are just fine.

> **Blocks**
>
> ![Screen Shot 2023-08-10 at 11.08.42 PM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/8/89a20c5788d70c361bedf5e162fb1a7905cf16ab.png)

> **Extension Code**
>
> ```auto
> //% weight=99 color="#FFB836" icon="\uf035"
> //% block="Literals"
> namespace literals {
> //% block="id $newid name $newname image $newimg"
> //% newimg.shadow=dialog_image_picker
> export function setPlayerData(newid: string, newname: string, newimg: Image): any {
> const objLiteral = {
> id: newid, 
> name: newname, 
> image: newimg 
> }
> return objLiteral
> }
> //% block="id $newid background image $newimg"
> //% newimg.shadow=dialog_image_picker
> export function setPlayerBackgroundData(newid: string, newimg: Image): any {
> const objLiteral = { 
> id: newid, 
> image: newimg 
> }
> return objLiteral
> }
> }
> 
> ```

I then implemented it into my code via JavaScript. Now **in the JavaScript client, everything functions smoothly.**

```auto
let PlayerList: any[] = [
    literals.setPlayerData("bon", "Bon", assets.image`myImage`)
]

```

An implementation example is this code sample:

```auto
let selectedPlayer = PlayerList.find(player => player.name === "Bon")

```

However when switching to the Blocks client, **that’s when issues begin.**

## Issue

When switching to Blocks, the array type changes to **number** :

```auto
let PlayerList: number[] = [literals.setPlayerData("bon", "Bon", assets.image`myImage`)]

// Or if I move things around for the sake of aesthetics...

let PlayerList: number[] = [
    literals.setPlayerData("bon", "Bon", assets.image`myImage`)
]

```

Now this seemingly automatic change obviously causes a plethora of errors:

![Screen Shot 2023-08-10 at 11.17.49 PM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/2/23309892dab8cef1b3b4b1727324f4743c90d80a.png)

**Any help is appreciated, my goal is to keep the code adjusted at an ‘any type array’.**

---

<div class="post-metadata">

### Author: ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)
#### Post date: [August 11, 2023, 5:03pm UTC](https://forum.makecode.com/t/code-works-in-javascript-but-not-blocks/21782/2 "2023-08-11T17:03:53Z")

</div>

There’s a simple fix for this: Don’t return the `any` type

Not only is it bad practice, it makes it very difficult for us to do type inference when compiling the blocks code. Looking at your code, there’s also no reason I see to use it. Being the any type will let people drag this block into any other block input, for example you can drag it into the blocks from the Sprites category:

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/b/bf1c52179871c63d09a0d097a36f15f9e569f8de.png)

Which is obviously an error.

Instead, define a type to return like this:

```auto
interface LiteralType {
    id: string;
    image: Image;
    name?: string;
}

```

---

<div class="post-metadata">

### Author: ![Unique](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unique/32/32724_2.png) [@Unique](https://forum.makecode.com/u/Unique)
#### Post date: [August 14, 2023, 4:12pm UTC](https://forum.makecode.com/t/code-works-in-javascript-but-not-blocks/21782/3 "2023-08-14T16:12:30Z")

</div>

This really helps, thank you!

---

<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: [August 14, 2023, 4:32pm UTC](https://forum.makecode.com/t/code-works-in-javascript-but-not-blocks/21782/4 "2023-08-14T16:32:17Z")

</div>

Moral of the story: Never use `any[]` in blocks
