Description
Is your feature request related to a problem? Please describe.
We have several HTTP + gRPC services where we have the same shape objects coming in on requests. We abstract the request/response handling with koa controllers / unary grpc functions, and share the logic after that.
The interfaces we make for use with the koa controller and service functions overlap exactly with the proto messages we write coming from grpc. However the types generated (the "permissive" type) has all properties optional.
There is a "restrictive" type, but it's always the message name with "__Output" on the end, which makes it very long and clunky to use.
We never want to use the permissive type. If we wanted optional properties, we would mark them as optional in the proto.
Describe the solution you'd like
A flag to suppress the output of the "permissive" type and to make the "restrictive" type use the name the permissive type would have.
Describe alternatives you've considered
Telling developers to always import the __Output
restrictive type is what we've been doing, but this has to be enforced in code review. If the permissive type wasn't generated at all, then it removes the need to look out for this.
Additional context
TypeScript doesn't think the types overlap
Proto file:
message UpdateLocationRequest {
int32 locationId = 1;
string name = 2;
string description = 3;
string locationNumber = 4;
string line1 = 5;
string line2 = 6;
string city = 7;
string state = 8;
string postal = 9;
int32 latitude = 10;
int32 longitude = 11;
string timezone = 12;
}
Generated types (from proto-loader-gen-types):
export interface UpdateLocationRequest {
'locationId'?: (number);
'name'?: (string);
'description'?: (string);
'locationNumber'?: (string);
'line1'?: (string);
'line2'?: (string);
'city'?: (string);
'state'?: (string);
'postal'?: (string);
'latitude'?: (number);
'longitude'?: (number);
'timezone'?: (string);
}
export interface UpdateLocationRequest__Output {
'locationId': (number);
'name': (string);
'description': (string);
'locationNumber': (string);
'line1': (string);
'line2': (string);
'city': (string);
'state': (string);
'postal': (string);
'latitude': (number);
'longitude': (number);
'timezone': (string);
}
Our Typescript interface (for koa controllers/service):
export interface LocationUpdate {
name: string;
description: string;
locationNumber: string;
line1: string;
line2?: string;
city: string;
state: string;
postal: string;
latitude: number;
longitude: number;
timezone: string;
}
Usage:
// service function
const updateLocation = async (locationId: number, location: LocationUpdate): Promise<void> => {
// ...
};
// koa controller
const updateLocationController = async (ctx: Context<LocationUpdate, void>) => {
const { locationId } = ctx.params;
await updateLocation(locationId, ctx.request.body);
};
// grpc unary handler
const updateLocationGrpcHandler = async (req: UpdateLocationRequest): Promise<void> => {
const { locationId, ...location } = req;
// type error here
await updateLocation(locationId, location);
};
Type error:
Argument of type '{ name?: string; description?: string; locationNumber?: string; line1?: string; line2?: string; city?: string; state?: string; postal?: string; latitude?: number; longitude?: number; timezone?: string; }' is not assignable to parameter of type 'LocationUpdate'.
Property 'name' is optional in type '{ name?: string; description?: string; locationNumber?: string; line1?: string; line2?: string; city?: string; state?: string; postal?: string; latitude?: number; longitude?: number; timezone?: string; }' but required in type 'LocationUpdate'.ts(2345)
Using restrictive type eliminates the error since the properties are not optional:
const updateLocationGrpcHandler = async (req: UpdateLocationRequest__Output): Promise<void> => {
const { locationId, ...location } = req;
// no more error
await updateLocation(locationId, location);
};
Activity