Live data from Hacker News

Show HN: Zero-codegen, no-compile TypeScript type inference from Protobufs

github.com

11–20 of 80 posts

Re: Show HN: Zero-codegen, no-compile TypeScript type inference from Protobufs

#11
post #4

The fact that the source is so small is wild. I would have expected a huge convoluted parsing library implemented in types. On the other hand, the fact that this is even possible is more wild. Instead of replacing JS with a proper statically-typed language, we're spending all this effort turning a preprocessor's type system into a turing-complete metalanguage. Pretty soon we'll be able to compile TypeScript entirely…

People have fussed the same of the C preprocessor, around the same time I and maybe you were born. (There's a pretty good chance I'm your parents' age, and nearly no chance you're the age of mine.)

Re: Show HN: Zero-codegen, no-compile TypeScript type inference from Protobufs

#14
This requires the whole `.proto` declaration inline in source a string constant. I'm not holding my breath on "Import non-js content"[1] getting approved, so that means you still have to use another build dependency, or manually keep the .proto files synchronized across multiple sources truth. In that light, it's not clear when this would be a benefit over straight-forward code gen. Cool POC hack though.

[1]: https://github.com/microsoft/TypeScript/issues/42219

Re: Show HN: Zero-codegen, no-compile TypeScript type inference from Protobufs

#15
post #5

Cool, but I assume not great for performance? Probably better to just stick with codegen

You're right that IDE/dev-time performance might be slower than using generated types since this relies on "dynamic" TypeScript inference rather than static codegen'd types. That said, depending on how your codegen works and how you're using protos at runtime, this approach might actually be faster at runtime. Types are stripped at compile-time and there’s no generated class or constructor logic — in the compiled out…

Typescript never generates classes or constructors that aren't present in source code. Whether or not constructors are present is completely independent from whether you're using code gen.

Re: Show HN: Zero-codegen, no-compile TypeScript type inference from Protobufs

#16
post #3

Cool, but I assume not great for performance? Probably better to just stick with codegen

Assuming you mean compiler/editor performance then yes I assume this wrecks it. Shouldn't matter for runtime though.

Right, I meant editor performance. None of the options should impact runtime performance anyway.

Re: Show HN: Zero-codegen, no-compile TypeScript type inference from Protobufs

#17
post #5

Cool, but I assume not great for performance? Probably better to just stick with codegen

You're right that IDE/dev-time performance might be slower than using generated types since this relies on "dynamic" TypeScript inference rather than static codegen'd types. That said, depending on how your codegen works and how you're using protos at runtime, this approach might actually be faster at runtime. Types are stripped at compile-time and there’s no generated class or constructor logic — in the compiled out…

> depending on how your codegen works and how you're using protos at runtime, this approach might actually be faster at runtime

If your codegen is introducing runtime overhead you should use a different codegen.

> type inference in VSCode seemed reasonably fast with the toy examples I was playing with

It usually is. It can become a problem in a real project that has a lot of stuff going on, though.

Re: Show HN: Zero-codegen, no-compile TypeScript type inference from Protobufs

#18
This would be even nicer if TypeScript added type inference for tagged template literals, like in this issue [1]. Then you could write:

    const schema = proto`
      syntax = "proto3";

      message Person { ... }
    `;

    type Person = typeof schema['Person'];
And you could get built-in schema validation with a sophisticated enough type definition for `proto`, nice syntax highlighting in many tools with a nested grammar.

We would love to see this feature in TypeScript to be able to have type-safe template in lit-html without an external tool.

The issue hasn't seen much activity lately, but it would be good to highlight this library as another use case.

[1]: https://github.com/microsoft/TypeScript/issues/33304

Re: Show HN: Zero-codegen, no-compile TypeScript type inference from Protobufs

#19
post #4

The fact that the source is so small is wild. I would have expected a huge convoluted parsing library implemented in types. On the other hand, the fact that this is even possible is more wild. Instead of replacing JS with a proper statically-typed language, we're spending all this effort turning a preprocessor's type system into a turing-complete metalanguage. Pretty soon we'll be able to compile TypeScript entirely…

I wish javascript had gone in the same direction as php with types.
Post reply on HN