Skip to content

Getting Started with TypeScript

Evan edited this page Dec 11, 2020 · 8 revisions

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 and the Bebop compiler as a dev dependency:

npm install bebop
npm install --save-dev bebop-tools

Write a schema, and compile it into TypeScript:

echo 'message Musician { 1-> string name; 2 -> uint16 age; }' > musician.bop

Open your package.json and add a task to the "scripts" field:

...
"scripts": {
    "compile-bebop": "bebopc --files musician.bop --ts schemas.ts"
}
...

Or even better, add a bebop.json to configure your project. Then your script will look like this:

...
"scripts": {
    "compile-bebop": "bebopc"
}
...
npm run compile-bebop

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