node-struct icon indicating copy to clipboard operation
node-struct copied to clipboard

Typescript declarations

Open hetsch opened this issue 8 years ago • 2 comments

Hi, currently I'm using your package in a Typescript project that I have inherited from a collegue. I really enjoy working with it, so far. What do you think about generating some Typescript declaration files?

I've never done it by hand so far, but Microsoft provides a generator that might be helpful dts-gen (https://github.com/Microsoft/dts-gen).

Calling dts-gen -m struct is not very helpful:

/** Declaration file generated by dts-gen */

export = struct;

declare function struct(...args: any[]): any;

declare namespace struct {
    // Circular reference from struct
    const Struct: any;

    const prototype: {
    };

}

hetsch avatar Dec 06 '17 11:12 hetsch

This might work:

/// <reference types="node" />

declare module 'struct' {
  import { Buffer } from "buffer";

  export class Struct {
    word8(name: string): void;
    word8Sle(name: string): void;
    word8Sbe(name: string): void;
    word16Sle(name: string): void;
    word16Sbe(name: string): void;
    word16Ule(name: string): void;
    word16Ube(name: string): void;
    word32Sle(name: string): void;
    word32Sbe(name: string): void;
    word32Ule(name: string): void;
    word32Ube(name: string): void;
    word64Sle(name: string): void;
    word64Sbe(name: string): void;
    word64Ule(name: string): void;
    word64Ube(name: string): void;
    floatle(name: string): void;
    floatbe(name: string): void;
    doublele(name: string): void;
    doublebe(name: string): void;
    chars(name: string, length: number, encoding?: string): void;
    charsnt(name: string, length: number, encoding?: string): void;
    array(name: string, length: number, type?: string | Struct): void;
    struct(name: string, struct: Struct): void;
    get(fieldName: string): any;
    set(fieldName: string, value: any): void;
    allocate(): void;
    _setBuff(buffer: Buffer): void;
    buffer(): Buffer;
    fields: object;
  }
}

hetsch avatar Dec 07 '17 10:12 hetsch

https://github.com/DefinitelyTyped/DefinitelyTyped/pull/40073

benallfree avatar Nov 01 '19 12:11 benallfree