kaitai_struct_compiler icon indicating copy to clipboard operation
kaitai_struct_compiler copied to clipboard

TypeScript language support

Open Theaninova opened this issue 2 years ago • 0 comments

resolves https://github.com/kaitai-io/kaitai_struct/issues/542

This is partially based on the JavaScript implementation, however with significant changes due to how TypeScript can make use of a lot more modern features, like actual classes that will be transpiled in case an older JS version should be targeted.

It might also be worth looking into generating .js + .d.ts instead...

Output will look something like this

/*
 * This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
 */
// @ts-ignore
import KaitaiStream from 'kaitai-struct/KaitaiStream'

export class BitsEnum {
  _is_le?: boolean;

  constructor(
    readonly _io: KaitaiStream,
    readonly _parent: unknown,
    readonly _root: BitsEnum,
  ) {
    this._root ??= this;

    this._read();
  }

  _read() {
    this.one = (this._io.readBitsIntBe(4)) as any
    this.two = (this._io.readBitsIntBe(8)) as any
    this.three = (this._io.readBitsIntBe(1)) as any
  }

  one: BitsEnum.Animal;
  two: BitsEnum.Animal;
  three: BitsEnum.Animal;
}
export namespace BitsEnum {
  export const enum Animal {
    CAT = 0,
    DOG = 1,
    HORSE = 4,
    PLATYPUS = 5,
  }
}

To be functional it might also require changes to the JavaScript runtime.

I've tested it against all files in /test/formats/, however nothing beyond that.

Theaninova avatar Feb 14 '23 21:02 Theaninova