Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Generate (sound) TypeScript and Flow declaration files #9

Merged
merged 6 commits into from
Dec 14, 2020

Commits on Dec 8, 2020

  1. Generate (correct) TypeScript and Flow declaration files

    This change adds Flow declaration types so that we can check the types
    of the generated code.
    
    Notably:
    
    - Adds a dependency to flowgen and calls it during compilation, which
      takes the `.d.ts` and creates a `.flow.js` based on it.
    - Passes in the `--force-number` flag to pbjs, which causes it to not
      emit numbers as possibly being `Long`, since this confuses Flow. This
      only affects JSDoc declarations, not the generated code.
    - Passes in the `--force-message` flag to pbjs. Otherwise, this emits
      invalid types: it tries to create code like this:
    
      ```TypeScript
      interface IMessage {
        field?: T|null;
      }
    
      class Message implements IMessage {
        field: T;  // notice the lack of optionality / nullness.
        ...
      }
      ```
    
      This violates the Liskov Substitution Principle, since `Message`
      cannot be passed to any place that accepts an `IMessage`, so
      TypeScript (correctly) rejects this as bogus. With this flag, the
      number of places that will accept an `IMessage` instead of a `Message`
      are limited to constructors, where we do want to have partial messages
      being passed in.
    
      This only affects JSDoc declarations, not the generated code.
    - Removes all `@implements` annotations from the JSDoc, which takes care
      of the very last `IMessage`/`Message` confusion. This, together with
      the above element take care of
      protobufjs/protobuf.js#837
    lhchavez committed Dec 8, 2020
    Configuration menu
    Copy the full SHA
    8046d3e View commit details
    Browse the repository at this point in the history

Commits on Dec 9, 2020

  1. Added a small tool to fix the JSDoc declarations to add some flexibility

    This still emits completely sound types (needed for Flow, since
    TypeScript is intentionally unsound), and also lets the callers do more
    idiomatic protobuf construction.
    lhchavez committed Dec 9, 2020
    Configuration menu
    Copy the full SHA
    d9efe01 View commit details
    Browse the repository at this point in the history
  2. Add another tool to make all Flow types exact types

    Hooray for type safety!
    lhchavez committed Dec 9, 2020
    Configuration menu
    Copy the full SHA
    37c7bb0 View commit details
    Browse the repository at this point in the history

Commits on Dec 10, 2020

  1. _Really_ make the interfaces also accept interfaces

    In a previous attempt to avoid changing types that are not interfaces
    (e.g. enums), the interfaces were also not converted. This is now fixed.
    lhchavez committed Dec 10, 2020
    Configuration menu
    Copy the full SHA
    cb315bb View commit details
    Browse the repository at this point in the history
  2. Make .create() forward its call to .fromObject()

    This creates objects with the correct type requirements AND outputs
    objects with the correct types.
    
    Type safety FTW!
    lhchavez committed Dec 10, 2020
    Configuration menu
    Copy the full SHA
    e174660 View commit details
    Browse the repository at this point in the history

Commits on Dec 14, 2020

  1. Avoid doing the constructor=>fromObject replacement outside of .create()

    This fixes an accidental infinite loop.
    lhchavez committed Dec 14, 2020
    Configuration menu
    Copy the full SHA
    455d2a6 View commit details
    Browse the repository at this point in the history