creditkarma / thrift-typescript

Generate TypeScript from Thrift IDL files
Apache License 2.0
155 stars 32 forks source link

Initial work for version 2 of thrift codegen #109

Closed kevin-greene-ck closed 6 years ago

kevin-greene-ck commented 6 years ago

The aim of v2 is to be more intelligent with the codegen for thrift-server. In v1 a struct, union or exception defined in Thrift source becomes a class in the output TypeScript.

struct MyStruct {
    1: string message;
}

Becomes...

export class MyStruct {
    public message: string;
    constructor(args: { message: string }) { ... }
    static read(input: TProtocol): MyStruct { ... }
    write(output: TProtocol): void { ... }
}

Something almost identical is generated for exceptions and unions.

There is weight and complexity to this that should be hidden from consumers. A struct in Thrift is more analogous to a TypeScript interface. A union is required to have one field and only one field. We have unions in TypeScript, but not with fields. We can can get compile-time guarantees around setting only one union field if we generate a TypeScript union of single-field interfaces. Exceptions do more naturally translate to classes. We can generate a class that extends Error and this would allow for more natural usage for consumers.