Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript language support #249

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Theaninova
Copy link

@Theaninova Theaninova commented Feb 14, 2023

resolves kaitai-io/kaitai_struct#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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for typescript generation
1 participant