Add a flag for proto-loader-gen-types to only output the restrictive type and remove the "__Output" from the name
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 35/100
- Tipo de issue
- Nueva funcionalidad
- Claridad
- Bien especificado
- Estado de actividad
- Estancado
- Stack tecnológico
- node.js, typescript
Línea de trabajo
Comienza localizando el punto de entrada de proto-loader-gen-types y sus opciones de generación existentes; después, sigue el flujo hasta donde se emiten las interfaces permisiva y restrictiva. Se considera terminado cuando un flag suprime la interfaz permisiva y da su nombre a la interfaz restrictiva, y se han verificado la salida generada y el comportamiento relevante.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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);
};
- Lenguaje dominante
- TypeScript
- Estrellas
- 4.8k
- Forks
- 716
- Merge medio
- 2 d 3 h
- PR fusionados (30 d)
- 10
Preparar el entorno
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de grpc/grpc-node
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
Los mantenedores suelen responder en 1 día
-
package: @grpc/grpc-js
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
grpc/grpc-node#2993 · 3 comentarios · 4 reacciones ·
Los mantenedores suelen responder en 1 día
-
Dificultad 3/5 1-2 días Aptitud para principiantes 65/100
grpc/grpc-node#3091 · 1 reacción ·
Los mantenedores suelen responder en 1 día
-
feature request
Dificultad 3/5 1-2 días Aptitud para principiantes 68/100
grpc/grpc-node#3077 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
Dificultad 3/5 1-2 días Aptitud para principiantes 76/100
grpc/grpc-node#3068 · 2 comentarios · 1 reacción ·
Los mantenedores suelen responder en 1 día
Todos los issues de grpc/grpc-node
Issues similares
-
bug
Dificultad 1/5 Menos de una hora Aptitud para principiantes 88/100
StabilityNexus/Fate-EVM-Frontend#153 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
code-yeongyu/oh-my-openagent#9039 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
Tencent/teamai-cli#862 ·
Los mantenedores suelen responder en 1 día
-
bug good first issue hacktoberfest redis
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
libredb/libredb-studio#1164 ·
Los mantenedores suelen responder en 1 día
-
flake
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
coder/xum#4920 · 2 comentarios ·
Los mantenedores suelen responder en 1 día