How OOP Developers Can Get To Know TypeScript Through Deno
For object-oriented programming (OOP) developers brought up on Java and C#, JavaScript code can be painful. Using TypeScript via Deno helps.
Oct 19th, 2024 8:00am by

Image via Unsplash+.
interface Pointlike { x: number; y: number; }
interface Named { name: string; }
function logPoint(point: Pointlike) {
console.log("x = " + point.x + ", y = " + point.y);
}
function logName(x: Named) {
console.log("Hello, " + x.name);
}
const obj = { x: 0, y: 0, name: "Origin", };
logPoint(obj);
logName(obj);
Installing Deno
On my MacBook, I used:
curl -fsSL https://deno.land/install.sh | sh
brew install deno
So I’ve got Deno 2 installed and hopefully a reasonably up-to-date TypeScript library.
I’ll start with creating a main.ts file, throw in my example code above and just run it:
So we have euthanized two birds with one stone: Proof that Deno just runs TypeScript as first-class code and that TypeScript works with structures that are not “nominal types.” Clearly, TypeScript thinks of our JSON object as two sets of values, each of which satisfies one interface. But that implies that types can’t be used as identities, like they can be in C#. So, there would be no real point in reflecting an object’s type, for example.
Here is another bit of TypeScript that, viewed from an OOP perspective, looks weird:
class Car {
drive() {
// hit the pedal to the floor
}
}
class Golfer {
drive() {
// hit the ball far
}
}
// No error?
let w: Car = new Golfer();
The equivalent would clearly be weird in C#, but in a TypeScript structural sense, both classes are indeed identical.
Let’s use the Deno project initializer to check the above.
So it appears that the project created tests, too:
I’ll overwrite the template code and create a test for our weird TypeScript example.
So, the test can be run directly, and there seems to be no exception error.
In this case, weird is good. Deno comes with lots of other TypeScript-specific support.
Deno Deploy
Deno offers you some free infrastructure deployment on its own V8 cloud, and a simple way to do it.
Free deployment is always good, plus Deno has given itself a commercial proposition.
Rather surprisingly, it has a deploy playground to give you an idea of what “low traffic” examples can be supported. For instance, you could just use Deno.serve to write a simple response server:
Press a button, and this is now deployed to https://cloudy-shark-26.deno.dev. So Deno playgrounds can be used to write and deploy small projects.
Deno also provides an example of using its simple key-value data store via the playground:
const kv = await Deno.openKv();
Deno.serve(async () => {
// increment a count using Deno KV
await kv.atomic().sum(["visitors"], 1n).commit();
// Get the latest visitor count
const count = await kv.get(["visitors"]);
return new Response(`You are TheNewStack DenoKV demo visitor number #${count.value}`);
});
Even though the playground deploys to many different “isolate” servers, it is connected to a single Playground id. This code opens the key-value store, uses the side effect of counting atomic operations, and saves the result as a BigInt in the key “visitors.” I could have changed the 1n to 2n if I wanted it to go up twice as fast. This deployed to here. I get a deployment dashboard too, so I can track all these playground projects.
Conclusion
I think I can see a lot of the old Heroku PaaS dynamism in this easy to access approach to both TypeScript projects and deployment. The AWS Lambda-like Playground service could prove to be an exceptionally cool prototyping tool, as well as a boon to educators. As for TypeScript, it very much isn’t OOP, but it still makes JavaScript a little less icky — so I will endeavor to use it more often when I can.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.
