#@method.returns(Field) does not compile

1 messages · Page 1 of 1 (latest)

desert heart
#

Using @method.returns decorator gives the error below. The error disappears when returns is removed.

src/KV.ts:39:4 - error TS2345: Argument of type 'ActionsKV' is not assignable to parameter of type 'SmartContract & { [x: string]: (...args: any) => Promise<Field>; }'.
  Type 'ActionsKV' is not assignable to type '{ [x: string]: (...args: any) => Promise<Field>; }'.
    Index signature for type 'string' is missing in type 'ActionsKV'.

39   @method.returns(Field) async get(key: PublicKey) {
      ~~~~~~~~~~~~~~~~~~~~~
worldly pawn
#

can you show the code? I doubt that .returns() is the problem, we use it all over the place

desert heart
#
  Field,
  Struct,
  method,
  state,
  State,
  SmartContract,
  Reducer,
  provable,
  PublicKey,
  Bool,
  Poseidon,
  Provable
} from 'o1js';

/**
 * Basic Example
 * See https://docs.minaprotocol.com/zkapps for more info.
 *
 * The Add contract initializes the state variable 'num' to be a Field(1) value by default when deployed.
 * When the 'update' method is called, the Add contract adds Field(2) to its 'num' contract state.
 *
 * This file is safe to delete and replace with your own contract.
 */


export class Option extends Struct({
  isSome: Bool,
  value: Field,
}) {}

export const KeyValuePair = provable({
  key: Field,
  value: Field,
});

export class StorageContract extends SmartContract {
  reducer = Reducer({
    actionType: KeyValuePair,
  });

  @method async set(key: PublicKey, value: Field) {
    this.reducer.dispatch({ key: Poseidon.hash(key.toFields()), value });
  }

  @method.returns(Option)
  async get(key: PublicKey) {
    let pendingActions = this.reducer.getActions({
      fromActionState: Reducer.initialActionState,
    });

    let keyHash = Poseidon.hash(key.toFields());

    let optionValue = this.reducer.reduce(
      pendingActions,
      Option,
      (state, action) => {
        let currentMatch = keyHash.equals(action.key);
        return {
          isSome: currentMatch.or(state.isSome),
          value: Provable.if(currentMatch, action.value, state.value),
        };
      },
      Option.empty(),
      { maxUpdatesWithActions: 5 }
    );

    return optionValue;
  }
}

#

code is a copy of map.ts from your feature/experimental-offchain-state fork.

desert heart
worldly pawn
#

Changing Option to Field seems wrong because the method actually returns an option

#

It has to match the method return type

desert heart
tawdry flint
#

Did you find a fix to this problem?

desert heart
urban pollen
#

try changing this

  async get(key: PublicKey) { ```
to 
```  @method.returns(Option)
  async get(key: PublicKey): Promise<Option> { ```
pretty sure stuff should work without explicitly defining return type of TS but kinda remember getting similar error when initially migrating to latest o1js
#

looking at my commits around that time I've had to update typescript in my package.json to "^5.4.3"

worldly pawn
#

hey @desert heart I copied your file into my editor, it doesn't have an error for me. I'm on TS 5.1

desert heart
sharp pulsar
desert heart