wharfkit / antelope

Core types, client interfaces, and other tools for working with Antelope-based blockchains.
Other
42 stars 23 forks source link

Found undefined for non-optional type: name #19

Closed DenisCarriere closed 2 years ago

DenisCarriere commented 2 years ago

Code seems to be working in the browser, however executing it locally with ts-node index.ts throws the following error:

/Users/denis/Github/typescript-i1q86h/node_modules/@greymass/eosio/src/serializer/encoder.ts:182
            throw new Error(`Found ${value} for non-optional type: ${type.typeName}`)
                  ^
EncodingError: Found undefined for non-optional type: name

Code Example

https://stackblitz.com/edit/typescript-i1q86h

Code snippet

import { Struct, Name, Asset, Action, NameType, AssetType } from '@greymass/eosio';

@Struct.type('transfer')
class Transfer extends Struct {
  @Struct.field('name') from!: Name;
  @Struct.field('name') to!: Name;
  @Struct.field('asset') quantity!: Asset;
  @Struct.field('string') memo!: string;
}

export function transferAction( from: NameType, to: NameType, quantity: AssetType, memo: string ): Action {
  return Action.from({
    authorization: [ { actor: from, permission: "active" } ],
    account: "eosio.token",
    name: 'transfer',
    data: Transfer.from({
      from,
      to,
      quantity,
      memo,
    }),
  })
}

const action = transferAction("myaccount", "toaccount", "1.0000 EOS", "foo");
console.log(action.toJSON())
jnordberg commented 2 years ago

You need to set your typescript target to es2020 or lower.

See https://github.com/microsoft/TypeScript/issues/44429 for more info

DenisCarriere commented 2 years ago

🙇‍♂️ Thanks, it worked!

Changed "target": "es2020" in tsconfig.json

$ ts-node index.ts
{
  account: Name { value: UInt64 { value: [BN] } },
  name: Name { value: UInt64 { value: [BN] } },
  authorization: [ PermissionLevel { actor: [Name], permission: [Name] } ],
  data: Bytes {
    array: Uint8Array(36) [
       0,   0, 200, 83,  83, 132, 140, 151,   0,
       0, 200,  83, 83, 132,  12, 205,  16,  39,
       0,   0,   0,  0,   0,   0,   4,  69,  79,
      83,   0,   0,  0,   0,   3, 102, 111, 111
    ]
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es2020",
    "moduleResolution": "node",
    "experimentalDecorators": true
  }
}