ts-py-struct
v1.2.3
Published
port python's struct to typescript.
Downloads
10
Readme
ts-py-struct
port python's struct to typescript.
Feature
- https://docs.python.org/3/library/struct.html
- Infer the arguments type of pack and the return value type of unpack from the format.
- Native size (
@
) same to LP64 - Omit 'p' format
- 's' format with string: encode with utf-8
Requirements
- Typescript 4.1.x or later
Usage
import { pack, unpack } from 'ts-py-struct'
//const packed: Uint8Array = pack('<hHl',7777, 65534) // Compile Error: Expected 4 arguments, but got 3.
const packed: Uint8Array = pack('<hHl', 7777, 65534, 3123213)
//const unpacked: [number, number] = unpack('<hHl',packed) // Compile Error: Type '[number, number, number]' is not assignable to type '[number]'. Source has 3 element(s) but target allows only 1.ts(2322)
const unpacked: [number, number, number] = unpack('<hHl', packed)
class interface
import { Struct } from 'ts-py-struct'
const s = new Struct('2c')
// const packed = s.pack('A', 22) // Compile Error: Argument of type 'number' is not assignable to parameter of type 'string'.ts(2345)
const packed = s.pack('A', '0')
const unpacked = s.unpack(packed) // unpacked type is [string, string]