JairusSW / as-json

The only JSON library you'll need for AssemblyScript. SIMD enabled
MIT License
79 stars 15 forks source link

A function whose declared type is not 'void' must return a value #39

Closed mariusa closed 1 year ago

mariusa commented 1 year ago

Getting this error at https://github.com/JairusSW/as-json/blob/40a04c1ab07408fa378d6b7bbbbfc339adfe0fed/assembly/src/json.ts#L404

function parseArray<T extends unknown[]>(data: string): T {

To fix, I've added these right before end of function:

    console.error(`json-as json.ts parseArray cannot find type ${typeof type}`)
    return []
mariusa commented 1 year ago

This used to work, but fails now with 0.5.15

import { JSON } from "json-as/assembly"

@json
class Vec2 {
    x: f32
    y: f32
}

@json
class Contact {
    name: string
}

@json
class Player {
    firstName: string
    lastName: string
    lastActive: i32[]
    age: i32
    pos: Vec2
    contacts: Contact[]
    isVerified: boolean
}

const player: Player = {
    firstName: "Emmet",
    lastName: "West",
    lastActive: [8, 27, 2022],
    contacts: [{ name: 'John' }],
    age: 23,
    pos: {
        x: -3.4,
        y: 1.2
    },
    isVerified: true
}

const stringified = JSON.stringify<Player>(player)
console.log('stringified')
console.log(stringified)
// {"firstName":"Emmet","lastName":"West","lastActive":[8,27,2022],"age":23,"pos":{"x":-3.4000000953674318,"y":1.2000000476837159},"isVerified":true}
// let stringified2 = `{"firstName":"John","lastName":"West","age":23}`

console.log('parsing')
const parsed = JSON.parse<Player>(stringified)
console.log(parsed.firstName)
console.log(parsed.contacts[0].name)
// console.log(Object.keys(parsed).length)

output:

parsing
Error: json-as json.ts parseArray cannot find type object
Emmet
abort: Index out of range in ~lib/array.ts(114:42)

Would be good to have some tests to run before publishing a new version ;)

JairusSW commented 1 year ago

Yep, as-pect broke on me, so I need to rewrite tests sometime soon. Also need more aggressive testing

mariusa commented 1 year ago

Hi, Happy New Year! Would you have an estimate for the fix, please? I've posted the reproduction above. Thanks

JairusSW commented 1 year ago

Oh my gosh, I'm so sorry! Busy with school this morning, but perhaps I can take a look this afternoon.

JairusSW commented 1 year ago

You know what, I just took a look and it didn't take long

mariusa commented 1 year ago

Thank you, you rock! Appreciate the release too.