Skip to content

Commit 0a3e624

Browse files
committed
fix!: use type union instead of optional for fields
Changes how nullability is expressed in the generated types. Previously every field was optional ```ts type Message = { field?: string; } ``` which is convenient as there is no need to specify every field that exists in the message when declaring it. However this have some unfortunate side effects, most notably that almost every type is assignable to other types. ```ts type MessageA = { foo?: string; } type MessageB = { bar?: string; } const a: MessageA = {} const b: MessageB = a; // ok because no field needs to be set in MessageB. ``` This can lead to subtle bugs when passing these types around. With this change every field is no longer optional, but `undefined` is a valid value for every field. ```ts type MessageA = { field: string | undefined; } ``` Fields belonging to a oneof is still typed as optional, because only one of these fields should ever be set. ```ts type MessageA = { foo: string | undefined; } type MessageB = { bar: string | undefined; } const a: MessageA = { foo: undefined }; const b: MessageB = a; // error ``` --- BREAKING CHANGE: Fields that could previously be omitted when declaring a variable, now needs to explicitly be set to `undefined`. Code that reads these fields will not break.
1 parent e27c51e commit 0a3e624

File tree

4 files changed

+214
-209
lines changed

4 files changed

+214
-209
lines changed

examples/proto/gen/typescript/einride/example/freight/v1/index.ts

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,32 @@
66
// [site][einride.example.freight.v1.Site].
77
export type Shipment = {
88
// The resource name of the shipment.
9-
name?: string;
9+
name: string | undefined;
1010
// The creation timestamp of the shipment.
11-
createTime?: wellKnownTimestamp;
11+
createTime: wellKnownTimestamp | undefined;
1212
// The last update timestamp of the shipment.
1313
// Updated when create/update/delete operation is shipment.
14-
updateTime?: wellKnownTimestamp;
14+
updateTime: wellKnownTimestamp | undefined;
1515
// The deletion timestamp of the shipment.
16-
deleteTime?: wellKnownTimestamp;
16+
deleteTime: wellKnownTimestamp | undefined;
1717
// The resource name of the origin site of the shipment.
1818
// Format: shippers/{shipper}/sites/{site}
19-
originSite?: string;
19+
originSite: string | undefined;
2020
// The resource name of the destination site of the shipment.
2121
// Format: shippers/{shipper}/sites/{site}
22-
destinationSite?: string;
22+
destinationSite: string | undefined;
2323
// The earliest pickup time of the shipment at the origin site.
24-
pickupEarliestTime?: wellKnownTimestamp;
24+
pickupEarliestTime: wellKnownTimestamp | undefined;
2525
// The latest pickup time of the shipment at the origin site.
26-
pickupLatestTime?: wellKnownTimestamp;
26+
pickupLatestTime: wellKnownTimestamp | undefined;
2727
// The earliest delivery time of the shipment at the destination site.
28-
deliveryEarliestTime?: wellKnownTimestamp;
28+
deliveryEarliestTime: wellKnownTimestamp | undefined;
2929
// The latest delivery time of the shipment at the destination site.
30-
deliveryLatestTime?: wellKnownTimestamp;
30+
deliveryLatestTime: wellKnownTimestamp | undefined;
3131
// The line items of the shipment.
32-
lineItems?: LineItem[];
32+
lineItems: LineItem[] | undefined;
3333
// Annotations of the shipment.
34-
annotations?: { [key: string]: string };
34+
annotations: { [key: string]: string } | undefined;
3535
};
3636

3737
// Encoded using RFC 3339, where generated output will always be Z-normalized
@@ -42,46 +42,46 @@ type wellKnownTimestamp = string;
4242
// A shipment line item.
4343
export type LineItem = {
4444
// The title of the line item.
45-
title?: string;
45+
title: string | undefined;
4646
// The quantity of the line item.
47-
quantity?: number;
47+
quantity: number | undefined;
4848
// The weight of the line item in kilograms.
49-
weightKg?: number;
49+
weightKg: number | undefined;
5050
// The volume of the line item in cubic meters.
51-
volumeM3?: number;
51+
volumeM3: number | undefined;
5252
};
5353

5454
// A shipper is a supplier or owner of goods to be transported.
5555
export type Shipper = {
5656
// The resource name of the shipper.
57-
name?: string;
57+
name: string | undefined;
5858
// The creation timestamp of the shipper.
59-
createTime?: wellKnownTimestamp;
59+
createTime: wellKnownTimestamp | undefined;
6060
// The last update timestamp of the shipper.
6161
// Updated when create/update/delete operation is performed.
62-
updateTime?: wellKnownTimestamp;
62+
updateTime: wellKnownTimestamp | undefined;
6363
// The deletion timestamp of the shipper.
64-
deleteTime?: wellKnownTimestamp;
64+
deleteTime: wellKnownTimestamp | undefined;
6565
// The display name of the shipper.
66-
displayName?: string;
66+
displayName: string | undefined;
6767
};
6868

6969
// A site is a node in a [shipper][einride.example.freight.v1.Shipper]'s
7070
// transport network.
7171
export type Site = {
7272
// The resource name of the site.
73-
name?: string;
73+
name: string | undefined;
7474
// The creation timestamp of the site.
75-
createTime?: wellKnownTimestamp;
75+
createTime: wellKnownTimestamp | undefined;
7676
// The last update timestamp of the site.
7777
// Updated when create/update/delete operation is performed.
78-
updateTime?: wellKnownTimestamp;
78+
updateTime: wellKnownTimestamp | undefined;
7979
// The deletion timestamp of the site.
80-
deleteTime?: wellKnownTimestamp;
80+
deleteTime: wellKnownTimestamp | undefined;
8181
// The display name of the site.
82-
displayName?: string;
82+
displayName: string | undefined;
8383
// The geographic location of the site.
84-
latLng?: googletype_LatLng;
84+
latLng: googletype_LatLng | undefined;
8585
};
8686

8787
// An object that represents a latitude/longitude pair. This is expressed as a
@@ -91,55 +91,55 @@ export type Site = {
9191
// standard</a>. Values must be within normalized ranges.
9292
export type googletype_LatLng = {
9393
// The latitude in degrees. It must be in the range [-90.0, +90.0].
94-
latitude?: number;
94+
latitude: number | undefined;
9595
// The longitude in degrees. It must be in the range [-180.0, +180.0].
96-
longitude?: number;
96+
longitude: number | undefined;
9797
};
9898

9999
// Request message for FreightService.GetShipper.
100100
export type GetShipperRequest = {
101101
// The resource name of the shipper to retrieve.
102102
// Format: shippers/{shipper}
103-
name?: string;
103+
name: string | undefined;
104104
};
105105

106106
// Request message for FreightService.ListShippers.
107107
export type ListShippersRequest = {
108108
// Requested page size. Server may return fewer shippers than requested.
109109
// If unspecified, server will pick an appropriate default.
110-
pageSize?: number;
110+
pageSize: number | undefined;
111111
// A token identifying a page of results the server should return.
112112
// Typically, this is the value of
113113
// [ListShippersResponse.next_page_token][einride.example.freight.v1.ListShippersResponse.next_page_token]
114114
// returned from the previous call to `ListShippers` method.
115-
pageToken?: string;
115+
pageToken: string | undefined;
116116
};
117117

118118
// Response message for FreightService.ListShippers.
119119
export type ListShippersResponse = {
120120
// The list of shippers.
121-
shippers?: Shipper[];
121+
shippers: Shipper[] | undefined;
122122
// A token to retrieve next page of results. Pass this value in the
123123
// [ListShippersRequest.page_token][einride.example.freight.v1.ListShippersRequest.page_token]
124124
// field in the subsequent call to `ListShippers` method to retrieve the next
125125
// page of results.
126-
nextPageToken?: string;
126+
nextPageToken: string | undefined;
127127
};
128128

129129
// Request message for FreightService.CreateShipper.
130130
export type CreateShipperRequest = {
131131
// The shipper to create.
132-
shipper?: Shipper;
132+
shipper: Shipper | undefined;
133133
};
134134

135135
// Request message for FreightService.UpdateShipper.
136136
export type UpdateShipperRequest = {
137137
// The shipper to update with. The name must match or be empty.
138138
// The shipper's `name` field is used to identify the shipper to be updated.
139139
// Format: shippers/{shipper}
140-
shipper?: Shipper;
140+
shipper: Shipper | undefined;
141141
// The list of fields to be updated.
142-
updateMask?: wellKnownFieldMask;
142+
updateMask: wellKnownFieldMask | undefined;
143143
};
144144

145145
// In JSON, a field mask is encoded as a single string where paths are
@@ -174,125 +174,125 @@ type wellKnownFieldMask = string;
174174
export type DeleteShipperRequest = {
175175
// The resource name of the shipper to delete.
176176
// Format: shippers/{shipper}
177-
name?: string;
177+
name: string | undefined;
178178
};
179179

180180
// Request message for FreightService.GetSite.
181181
export type GetSiteRequest = {
182182
// The resource name of the site to retrieve.
183183
// Format: shippers/{shipper}/sites/{site}
184-
name?: string;
184+
name: string | undefined;
185185
};
186186

187187
// Request message for FreightService.ListSites.
188188
export type ListSitesRequest = {
189189
// The resource name of the parent, which owns this collection of sites.
190190
// Format: shippers/{shipper}
191-
parent?: string;
191+
parent: string | undefined;
192192
// Requested page size. Server may return fewer sites than requested.
193193
// If unspecified, server will pick an appropriate default.
194-
pageSize?: number;
194+
pageSize: number | undefined;
195195
// A token identifying a page of results the server should return.
196196
// Typically, this is the value of
197197
// [ListSitesResponse.next_page_token][einride.example.freight.v1.ListSitesResponse.next_page_token]
198198
// returned from the previous call to `ListSites` method.
199-
pageToken?: string;
199+
pageToken: string | undefined;
200200
};
201201

202202
// Response message for FreightService.ListSites.
203203
export type ListSitesResponse = {
204204
// The list of sites.
205-
sites?: Site[];
205+
sites: Site[] | undefined;
206206
// A token to retrieve next page of results. Pass this value in the
207207
// [ListSitesRequest.page_token][einride.example.freight.v1.ListSitesRequest.page_token]
208208
// field in the subsequent call to `ListSites` method to retrieve the next
209209
// page of results.
210-
nextPageToken?: string;
210+
nextPageToken: string | undefined;
211211
};
212212

213213
// Request message for FreightService.CreateSite.
214214
export type CreateSiteRequest = {
215215
// The resource name of the parent shipper for which this site will be created.
216216
// Format: shippers/{shipper}
217-
parent?: string;
217+
parent: string | undefined;
218218
// The site to create.
219-
site?: Site;
219+
site: Site | undefined;
220220
};
221221

222222
// Request message for FreightService.UpdateSite.
223223
export type UpdateSiteRequest = {
224224
// The site to update with. The name must match or be empty.
225225
// The site's `name` field is used to identify the site to be updated.
226226
// Format: shippers/{shipper}/sites/{site}
227-
site?: Site;
227+
site: Site | undefined;
228228
// The list of fields to be updated.
229-
updateMask?: wellKnownFieldMask;
229+
updateMask: wellKnownFieldMask | undefined;
230230
};
231231

232232
// Request message for FreightService.DeleteSite.
233233
export type DeleteSiteRequest = {
234234
// The resource name of the site to delete.
235235
// Format: shippers/{shipper}/sites/{site}
236-
name?: string;
236+
name: string | undefined;
237237
};
238238

239239
// Request message for FreightService.GetShipment.
240240
export type GetShipmentRequest = {
241241
// The resource name of the shipment to retrieve.
242242
// Format: shippers/{shipper}/shipments/{shipment}
243-
name?: string;
243+
name: string | undefined;
244244
};
245245

246246
// Request message for FreightService.ListShipments.
247247
export type ListShipmentsRequest = {
248248
// The resource name of the parent, which owns this collection of shipments.
249249
// Format: shippers/{shipper}
250-
parent?: string;
250+
parent: string | undefined;
251251
// Requested page size. Server may return fewer shipments than requested.
252252
// If unspecified, server will pick an appropriate default.
253-
pageSize?: number;
253+
pageSize: number | undefined;
254254
// A token identifying a page of results the server should return.
255255
// Typically, this is the value of
256256
// [ListShipmentsResponse.next_page_token][einride.example.freight.v1.ListShipmentsResponse.next_page_token]
257257
// returned from the previous call to `ListShipments` method.
258-
pageToken?: string;
258+
pageToken: string | undefined;
259259
};
260260

261261
// Response message for FreightService.ListShipments.
262262
export type ListShipmentsResponse = {
263263
// The list of shipments.
264-
shipments?: Shipment[];
264+
shipments: Shipment[] | undefined;
265265
// A token to retrieve next page of results. Pass this value in the
266266
// [ListShipmentsRequest.page_token][einride.example.freight.v1.ListShipmentsRequest.page_token]
267267
// field in the subsequent call to `ListShipments` method to retrieve the next
268268
// page of results.
269-
nextPageToken?: string;
269+
nextPageToken: string | undefined;
270270
};
271271

272272
// Request message for FreightService.CreateShipment.
273273
export type CreateShipmentRequest = {
274274
// The resource name of the parent shipper for which this shipment will be created.
275275
// Format: shippers/{shipper}
276-
parent?: string;
276+
parent: string | undefined;
277277
// The shipment to create.
278-
shipment?: Shipment;
278+
shipment: Shipment | undefined;
279279
};
280280

281281
// Request message for FreightService.UpdateShipment.
282282
export type UpdateShipmentRequest = {
283283
// The shipment to update with. The name must match or be empty.
284284
// The shipment's `name` field is used to identify the shipment to be updated.
285285
// Format: shippers/{shipper}/shipments/{shipment}
286-
shipment?: Shipment;
286+
shipment: Shipment | undefined;
287287
// The list of fields to be updated.
288-
updateMask?: wellKnownFieldMask;
288+
updateMask: wellKnownFieldMask | undefined;
289289
};
290290

291291
// Request message for FreightService.DeleteShipment.
292292
export type DeleteShipmentRequest = {
293293
// The resource name of the shipment to delete.
294294
// Format: shippers/{shipper}/shipments/{shipment}
295-
name?: string;
295+
name: string | undefined;
296296
};
297297

298298
// This API represents a simple freight service.

0 commit comments

Comments
 (0)