When we generate typescript interfaces, we generate interfaces like this:
export declare namespace domain {
type AssetLocation = proto.example.locations.IAssetLocation &
protobuf.Message<proto.example.locations.IAssetLocation>;
type CheckedIn = proto.example.assets.ICheckedIn &
protobuf.Message<proto.example.assets.ICheckedIn>;
}
export declare namespace AssetsByLocation {
type State = domain.AssetLocation;
type Events = domain.CheckedIn;
type UpdateHandlers = {
UpdateLocation: (
event: domain.CheckedIn,
state: State | undefined,
ctx: View.UpdateHandlerContext
) => State;
};
}
So, the UpdateLocation callback has to return something that implements both proto.example.locations.IAssetLocation and protobuf.Message<proto.example.locations.IAssetLocation>. The problem is, the protobuf compiler doesn't generate anything that implements both of these, it generates this:
/** Properties of an AssetLocation. */
interface IAssetLocation {
/** AssetLocation assetId */
assetId?: (string|null);
/** AssetLocation location */
location?: (string|null);
}
/** Represents an AssetLocation. */
class AssetLocation implements IAssetLocation {
/**
* Constructs a new AssetLocation.
* @param [properties] Properties to set
*/
constructor(properties?: example.locations.IAssetLocation);
/** AssetLocation assetId. */
public assetId: string;
/** AssetLocation location. */
public location: string;
So, in my UpdateLocation callback, I can instantiate an AssetLocation, but I can't return it, that will fail type checking. There's nothing that I can return. What we tell people to do is to do this:
const AssetLocation = view.lookupType("example.locations.AssetLocation");
const assetLocation = AssetLocation.create({assetId: "my-id"});
And that works, but the problem is that AssetLocation has a type of Message<{}>, so now we have lost type safety. I could do:
const AssetLocation = view.lookupType("example.locations.AssetLocation");
const assetLocation = AssetLocation.create({foo: "bar"});
That doesn't compile if I used the generated AssetLocation class, but does compile, and then fails silently at runtime, with the approach we force users to use.
The fact is, for these generated classes, we know exactly what type all the classes should be returning, so the metadata that we require to be there and extract from protobuf.Message shouldn't be necessary.
When we generate typescript interfaces, we generate interfaces like this:
So, the
UpdateLocationcallback has to return something that implements bothproto.example.locations.IAssetLocationandprotobuf.Message<proto.example.locations.IAssetLocation>. The problem is, the protobuf compiler doesn't generate anything that implements both of these, it generates this:So, in my
UpdateLocationcallback, I can instantiate anAssetLocation, but I can't return it, that will fail type checking. There's nothing that I can return. What we tell people to do is to do this:And that works, but the problem is that
AssetLocationhas a type ofMessage<{}>, so now we have lost type safety. I could do:That doesn't compile if I used the generated
AssetLocationclass, but does compile, and then fails silently at runtime, with the approach we force users to use.The fact is, for these generated classes, we know exactly what type all the classes should be returning, so the metadata that we require to be there and extract from
protobuf.Messageshouldn't be necessary.