How to Get TypeScript Types from a .proto File
Plenty of backends define their API in Protocol Buffers but hand the browser plain JSON, through grpc-gateway, Connect or a hand-written REST layer. The front end then needs types for that JSON, not message classes. This page reads the .proto file and writes TypeScript interfaces that match what protobuf's JSON mapping actually sends over the wire.
Paste or upload the .proto file
Paste a schema into the left editor, upload a .proto file (up to 10 MB), or press Sample. proto2, proto3 and edition files are read, and a snippet without a syntax line is treated as proto3. The page needs the schema itself: if you paste Protobuf data (text format or Base64) or plain JSON, it points you to the page that handles it.
Example: a small order schema
syntax = "proto3";
package shop.v1;
message Order {
int64 id = 1;
string customer_id = 2;
repeated string tags = 3;
optional string coupon = 4;
Status status = 5;
}
enum Status {
STATUS_UNSPECIFIED = 0;
STATUS_PAID = 1;
}
Read the TypeScript types
Each message becomes an exported interface keyed by its JSON field names, each enum becomes a union of its value names, and nested messages get names like Order_LineItem. A short note above the code flags anything worth a second look, such as a service that was left out or a type imported from a file you did not paste. A mistake in the .proto file shows up as an error with its line and column instead.
What the page writes for that schema
customer_id turns into the customerId key, the int64 id is a string, and Status is a union of its two names.
// TypeScript types for the JSON form of these messages: what protobuf's JSON mapping writes
// (protojson in Go, JsonFormat in Java, json_format in Python, protobufjs toJSON with longs
// as strings, grpc-gateway). Keys are the JSON names. Fields are optional because protobuf
// leaves out fields that hold their default value. These are types only: no message classes,
// no binary encoding. For those, run protoc with ts-proto or protobuf-ts.
//
// const value = JSON.parse(text) as Order;
//
// Note: 64-bit integer fields (int64, uint64, sint64, fixed64, sfixed64) are typed string, because protobuf's JSON writes them as strings so long IDs such as 8901240544102066246 keep every digit. Readers also accept a plain number, but JavaScript rounds numbers above 2^53, so keep these values as strings, or use BigInt(value) when you need to calculate.
//
// Note: Enums are typed as their value names, which is what protobuf's JSON writes. A number the .proto does not list is written as a plain number, so add | number to a field if your data can contain one.
/** The message shop.v1.Order. */
export interface Order {
id?: string;
customerId?: string; // customer_id in the .proto file
tags?: string[];
coupon?: string;
status?: Status;
}
/** The enum shop.v1.Status. protobuf's JSON writes the value name. */
export type Status =
| "STATUS_UNSPECIFIED"
| "STATUS_PAID";
Copy or download protobuf-types.ts
Copy the code or download protobuf-types.ts into your project. It is plain TypeScript with no imports, so it needs no package at all and passes tsc --strict.
import type { Order } from './protobuf-types';
const res = await fetch('/v1/orders/8901240544102066246');
const order = (await res.json()) as Order;
console.log(order.id); // "8901240544102066246", every digit
const nextId = BigInt(order.id ?? '0') + 1n; // 8901240544102066247n
const paid = order.status === 'STATUS_PAID'; // a missing status means STATUS_UNSPECIFIEDNeed message classes, binary encoding or a gRPC client too? Run protoc with ts-proto or protobuf-ts on the same file. To check a schema before converting it, use the Proto File Validator. If all you have is a JSON response, JSON to TypeScript builds types from the data, and Protobuf to JSON reads Protobuf text format or binary data.
Frequently Asked Questions
What exactly do these types describe?
The JSON form of your messages, the one protobuf's own JSON mapping produces: protojson in Go, JsonFormat in Java, json_format in Python, grpc-gateway and Connect when they answer with JSON. Keys are the JSON names, so a field called customer_id appears as customerId (or whatever json_name says). We checked the output against JSON written by Google's protobuf package: it is accepted by the generated interfaces with tsc --strict, while wrong types and unknown keys are rejected.
Why is an int64 field typed string instead of number?
Because that is how protobuf writes 64-bit integers in JSON, and for a good reason: a JavaScript number only holds whole numbers exactly up to 2^53, so an ID like 8901240544102066246 would come back as 8901240544102066000 if it went through a number. Keeping it a string keeps every digit. When you need to do arithmetic, BigInt(order.id) gives you the exact value. 32-bit types such as int32 and uint32 stay number.
Why is every field optional?
protobuf leaves a field out of the JSON when it holds its default value: 0, an empty string, false, an empty list or the first enum value. A response with status STATUS_UNSPECIFIED simply has no status key, so the types mark fields with a question mark and you decide what a missing key means. The one exception is a proto2 required field, which is always written and therefore not optional.
How are enums, maps, oneofs and well-known types written?
Enums become a union of their value names, such as 'STATUS_UNSPECIFIED' | 'STATUS_PAID'. map<K, V> becomes an index signature with string keys, since JSON keys are always strings. Every member of a oneof is listed under a comment, because only one of them can be present. Timestamp, Duration and FieldMask are strings, Struct, Value and ListValue use a small JsonValue type, and the wrapper types such as Int64Value use the type of the value they wrap. float and double fields may also hold "NaN", "Infinity" or "-Infinity", so they use a ProtoFloat type that allows those three strings.
Is this the same as running protoc with ts-proto or protobuf-ts?
No. Those plugins generate full message classes with binary encoding and gRPC clients, and you need them for the wire format. This page only writes type declarations for the JSON, which is often all a browser app or a REST client needs, and it works without installing anything. Services are left out with a note for the same reason.
Is my .proto file uploaded anywhere?
No. The file is read and converted in your browser and is not sent to our server. Nothing is kept either: a reload starts with an empty editor.