From a803b6d908e4a8a3934ec6bd691257e7ceeff64c Mon Sep 17 00:00:00 2001 From: Adam A Date: Thu, 9 May 2024 21:36:01 +0300 Subject: [PATCH] release(borshest@1): pass TS strict mode --- borshest/borsh-base.ts | 8 ++++---- borshest/borsh-collection.ts | 4 ++-- borshest/borsh-number.ts | 2 +- borshest/borsh-struct.ts | 6 ++++-- borshest/borshest.ts | 12 +++++++----- borshest/package.json | 2 +- borshest/tsconfig.json | 8 +++++++- 7 files changed, 26 insertions(+), 16 deletions(-) diff --git a/borshest/borsh-base.ts b/borshest/borsh-base.ts index bb2f8139..1dff4812 100644 --- a/borshest/borsh-base.ts +++ b/borshest/borsh-base.ts @@ -1,10 +1,10 @@ export type AnyField = { - encode (buffer: Writer, value: unknown) + encode (buffer: Writer, value: unknown): void decode (buffer: Reader): unknown } export type Field = { - encode (buffer: Writer, value: T) + encode (buffer: Writer, value: T): void decode (buffer: Reader): T } @@ -46,7 +46,7 @@ export class Writer { if (native) { const [toCall, size] = native this.grow(size); - this.view[toCall](this.offset, value, true); + ;(this.view[toCall as keyof DataView] as Function)(this.offset, value, true); this.offset += size; } else { throw new Error(`writeNumber got invalid type hint: ${type}`) @@ -95,7 +95,7 @@ export class Reader { if (native) { const [toCall, size] = native this.assertEnough(size) - const ret = this.view[toCall](this.offset, true); + const ret = (this.view[toCall as keyof DataView] as Function)(this.offset, true); this.offset += size return ret } else { diff --git a/borshest/borsh-collection.ts b/borshest/borsh-collection.ts index b308cbbb..c1d7f51d 100644 --- a/borshest/borsh-collection.ts +++ b/borshest/borsh-collection.ts @@ -37,7 +37,7 @@ export const vec = (element: Field): Field => ({ }) export const zVec = (element: Field) => ({ - encode (buffer, value) { + encode (buffer: never, value: T[]) { throw new Error('encode zVec: not implemented') }, decode (buffer: Reader): T[] { @@ -76,7 +76,7 @@ export const map = (k: Field, v: Field) buffer.writeNumber(keys.length, 'u32') // 4 bytes for length for (const key of keys) { // store key/values k.encode(buffer, key as K) - v.encode(buffer, isMap ? value.get(key as K) : value[key as K]) + v.encode(buffer, (isMap ? value.get(key as K) : value[key as K]) as V) } }, decode (buffer: Reader): Map { diff --git a/borshest/borsh-number.ts b/borshest/borsh-number.ts index 1e3fdd0d..1e1a3f77 100644 --- a/borshest/borsh-number.ts +++ b/borshest/borsh-number.ts @@ -19,7 +19,7 @@ export const unsigned = (bytes: number): Field => { encode (buffer: Writer, value: bigint) { const chunk = new Uint8Array(bytes); for (let i = 0; i < bytes; i++) { - buffer[i] = Number(value & 0xFFn); + ;(buffer as unknown as Array)[i] = Number(value & 0xFFn); value = value >> 8n; } buffer.write(chunk) diff --git a/borshest/borsh-struct.ts b/borshest/borsh-struct.ts index 69c40a15..b6ec2cbf 100644 --- a/borshest/borsh-struct.ts +++ b/borshest/borsh-struct.ts @@ -23,14 +23,16 @@ export const struct = (...fields: [string, AnyField][]): Field => { return { encode (buffer: Writer, value: T) { - for (const [key, element] of fields) element.encode(buffer, value[key]) + for (const [key, element] of fields) { + element.encode(buffer, value[key as keyof T]) + } }, decode (buffer: Reader): T { const result: Partial = {}; for (const [key, element] of fields) { try { - result[key] = element.decode(buffer) + result[key as keyof typeof result] = element.decode(buffer) as typeof result[keyof T] } catch (e) { ;(e as any).structPath ??= [] ;(e as any).structPath.unshift(key) diff --git a/borshest/borshest.ts b/borshest/borshest.ts index 479d12c7..7958ed5f 100644 --- a/borshest/borshest.ts +++ b/borshest/borshest.ts @@ -5,7 +5,9 @@ import { struct } from './borsh-struct' export type Fields = [string, AnyField][] export function encode (schema: Field, decoded: T): Uint8Array { - return schema.encode(new Writer(), decoded) + const writer = new Writer() + schema.encode(writer, decoded) + return new Uint8Array(writer.buffer) } export function decode (schema: Field, encoded: Uint8Array|Array): T { @@ -13,15 +15,15 @@ export function decode (schema: Field, encoded: Uint8Array|Array) return schema.decode(new Reader(encoded)) } -export function Struct (...fields) { +export function Struct (...fields: [string, AnyField][]) { const schema = struct(...fields) return class Struct { static decode (encoded: Uint8Array) { - return new this(decode(schema, encoded)) + return new this(decode(schema, encoded) as Record) } - constructor (data) { + constructor (data: Record) { for (const [key, _] of fields) { - this[key] = data[key] + Object.assign(this, { [key]: data[key] }) } } } diff --git a/borshest/package.json b/borshest/package.json index 90f811ea..d02c2a61 100644 --- a/borshest/package.json +++ b/borshest/package.json @@ -4,7 +4,7 @@ "type": "module", "bugs": "https://github.com/hackbg/toolbox/issues", "files": [ "README.md", "*.ts" ], - "version": "1.0.0-rc.1", + "version": "1.0.0", "license": "MIT", "keywords": [ "borsh", "zcash", "encoding", "decoding", "binary", "format", "fadroma", "namada" ], "homepage": "https://github.com/hackbg/toolbox", diff --git a/borshest/tsconfig.json b/borshest/tsconfig.json index ed957230..77237612 100644 --- a/borshest/tsconfig.json +++ b/borshest/tsconfig.json @@ -1,2 +1,8 @@ -{ "extends": "../tsconfig.json", "include": [ "*.ts" ] } +{ + "extends": "../tsconfig.json", + "include": [ "*.ts" ], + "compilerOptions": { + "strict": true + } +}