Skip to content

Getting Started with TypeScript

Andrew Sampson edited this page Dec 9, 2020 · 8 revisions

⚠️ Heads up! This tutorial assumes you've installed the compiler, bebopc, and it's on your PATH. If you haven't yet, go to Installing the compiler first.

Suppose you have a little TypeScript project set up with npm.

mkdir bebop-example
cd bebop-example
npm init -y
npm install -D typescript
npm install @types/node
tsc --init --target esnext

Add the Bebop runtime as a dependency:

npm install bebop

Write a schema, and compile it into TypeScript:

echo 'message Musician { 1-> string name; 2 -> uint16 age; }' > musician.bop
bebopc --lang ts --files musician.bop --out schemas.ts

Write and run code using your schema:

echo 'import { IMusician, Musician } from "./schemas";' > index.ts
echo 'console.log(Musician.encode({ name: "Charlie", age: 28 }));' >> index.ts
tsc && node index.js

(Output:)

Uint8Array(20) [
   16,   0,  0,   0,  1,   7,   0,
    0,   0, 67, 104, 97, 114, 108,
  105, 101,  2,  28,  0,   0
]

Clone this wiki locally