Loading TypeScript to Protobuf Converter...
Please wait a moment

How to Convert TypeScript to Protobuf

Step 1

Paste or upload TypeScript

Paste interfaces, type aliases, enums or classes, upload a .ts file, or click Sample to load an example. The page reads the type side of your code the way the TypeScript handbook describes interfaces and type aliases. Import lines, function bodies and runtime values are ignored, so you can paste a whole types.ts or a models file.

•
Interfaces and type aliases: every property becomes a field, in the order you declared it
•
Enums and literal unions: an enum, or a union like 'light' | 'dark', becomes a proto enum
•
Not types: a JSON object or a data example has no type information; use JSON to Protobuf for that
export enum Role { ADMIN, EDITOR, VIEWER }

export interface Address {
  street: string;
  postalCode: string;
}

export interface User {
  userId: bigint;
  name: string;
  role: Role;
  address?: Address;
  tags: string[];
}
Step 2

Read the generated schema

The proto3 schema appears on the right as you type. Check the comment above any field that was kept as string or renamed, and the "Valid Schema" badge that shows only after a real .proto parser accepts the output. The example above becomes the proto3 schema below. bigint maps to int64, the enum gets a _UNSPECIFIED = 0 value proto3 needs, and postalCode becomes postal_code. Field numbers follow the order of the properties, so keep them once the schema is in use; renumbering a field breaks messages that were already written.

syntax = "proto3";

package generated;

// Role: proto3 needs 0 for the default, so values start at 1 and the TypeScript values are not kept
enum Role {
  ROLE_UNSPECIFIED = 0;
  ROLE_ADMIN = 1;
  ROLE_EDITOR = 2;
  ROLE_VIEWER = 3;
}

message Address {
  string street = 1;
  string postal_code = 2;
}

message User {
  int64 user_id = 1;
  string name = 2;
  Role role = 3;
  Address address = 4;
  repeated string tags = 5;
}
Step 3

Copy or download the .proto file

Copy the schema or download it as converted.proto, then run protoc to generate code for TypeScript, Go, Java or any other language. The protoc compiler turns one .proto file into code for many languages. The schema here holds messages and enums; if you need a gRPC API, add the service and rpc lines yourself.

# install the TS plugin, then:
protoc --ts_proto_out=. converted.proto

# or generate for another language
protoc --go_out=. --java_out=. converted.proto

Frequently Asked Questions

Which TypeScript declarations does it read?

Interfaces and type aliases that describe an object (type X = { ... }) become messages, with every property as a field in the order you wrote it. enum declarations and string- or number-literal union types (type Status = 'open' | 'closed') become proto enums. A class is read too: its public, typed instance fields become fields, while methods, static members and private fields are skipped. An inline object inside a field, like settings: { theme: string }, turns into its own nested message.

How are TypeScript types mapped to proto types?

string stays string, boolean becomes bool, and an array (T[], Array<T> or Set<T>) becomes a repeated field. Record<string, V>, Map<string, V> and an index signature become map<string, V>. An optional field (name?) or a type that allows null or undefined becomes optional. bigint becomes int64. TypeScript has a single number type, so number becomes double; when the field name looks like a whole-number id, a comment points out that int64 or uint64 may fit better. Date becomes google.protobuf.Timestamp and the matching import line is added.

Are big numbers kept exactly?

Yes. A literal such as 8901240544102066246 in a union, or a string literal like '00501' or '14.50', is written into the comment exactly as you typed it, with no rounding. A bigint field maps to int64, which holds an id of that size. A plain number maps to double, so if a field really stores an id past 2^53 the comment suggests switching it to int64.

What happens to a union of objects, a tuple or a generic?

Proto3 has no exact match for some TypeScript shapes, so instead of guessing, the field is kept as string with a short comment saying why. A union of different types suggests a oneof; a tuple with mixed items, a generic type parameter, a function type or any becomes string with a note naming the typed alternative. A homogeneous tuple such as [number, number] becomes a repeated field.

Does it generate gRPC services?

No. The page writes messages and enums only, which is what your interfaces and types describe. Add any service { ... } rpc definitions yourself after you have the schema. Every output is checked with a real .proto parser before it is shown, so what you copy is valid proto3.

Is my TypeScript sent anywhere?

No. The conversion runs in your browser, nothing is uploaded, and nothing is kept once you leave: reloading the page gives you an empty editor. You can disconnect from the network after the page loads and it still works.

Going the other way? Protobuf to TypeScript turns a .proto file into interfaces, and JSON to Protobuf builds a schema from JSON data. For other languages, try Python to Protobuf or C# to Protobuf.