near / borsh-js

TypeScript/JavaScript implementation of Binary Object Representation Serializer for Hashing
Apache License 2.0
112 stars 38 forks source link

deserialize returns any because deserializeStruct isn't generic #43

Closed rtviii closed 10 months ago

rtviii commented 2 years ago

My editor didn't pick up an annotation on deserialize so I wondered whether deserialize could be made generic over classType so that it actually returns the thing it is trying to deserializing the buffer into but apparently [it is already generic over classType! ](https://github.com/near/borsh-js/blob/261d9cde21665de5a9e2c75ec2509c0bfb7030bf/borsh-ts/index.ts#L440-L445)

I'm not sure if i'm not seeign some obvious blocker to this, but from a cursory look at it – deserialize still always returns any because the deserializeStruct(...which deserialize calls?) [doesn't have a return type](https://github.com/near/borsh-js/blob/261d9cde21665de5a9e2c75ec2509c0bfb7030bf/borsh-ts/index.ts#L397-L422). Is that true?

I'm doing a tutorial on figment and at least that's how their borsh/lib type declarations read, maybe their version is lagging behind?

 /// <reference types="node" />
 import BN from 'bn.js';
 export declare function baseEncode(value: Uint8Array | string): string;
 export declare function baseDecode(value: string): Buffer;
 export declare type Schema = Map<Function, any>;
 export declare class BorshError extends Error 

 { originalMessage: string; fieldPath: string[]; constructor(message: string); addToFieldPath(fieldName: string): void; } 

export declare class BinaryWriter 

 { buf: Buffer; length: number; constructor(); maybeResize(): void; writeU8(value: number): void; writeU16(value: number): void; writeU32(value: number): void; writeU64(value: number | BN): void; writeU128(value: number | BN): void; writeU256(value: number | BN): void; writeU512(value: number | BN): void; private writeBuffer; writeString(str: string): void; writeFixedArray(array: Uint8Array): void; writeArray(array: any[], fn: any): void; toArray(): Uint8Array; } 

export declare class BinaryReader 

 { buf: Buffer; offset: number; constructor(buf: Buffer); readU8(): number; readU16(): number; readU32(): number; readU64(): BN; readU128(): BN; readU256(): BN; readU512(): BN; private readBuffer; readString(): string; readFixedArray(len: number): Uint8Array; readArray(fn: any): any[]; } 

export declare function serialize(schema: Schema, obj: any): Uint8Array;
 export declare function deserialize(schema: Schema, classType: any, buffer: Buffer): any;
 export declare function deserializeUnchecked(schema: Schema, classType: any, buffer: Buffer): any;
gagdiez commented 10 months ago

should be fixed now, please open again if not