RSS Amplifier

citizen428.net · Oct 12, 2024

Deno + Typescript == Scripting ❤️

0
Sign in to vote or save

citizen428.net

- typescript deno

Over the past year or so, something unexpected happened: TypeScript has seemingly become my most used scripting language, thanks in no small part to Deno.

There are quite a few reasons for this:

  • It’s well suited for my recent scripting needs which involved a lot of ETL tasks with CSV and JSON files, as well as REST APIs as data sources. I also had to write quite a few data migrations for a Firestore database, another area where TypeScript seemed like a good choice. In all these cases it’s very convenient that Deno can directly import JSON:
    import sites from "sites.json" with { type: "json" }
    
  • I’m quite comfortable with it and it’s a much saner experience than writing shell scripts, with better tooling.
  • The gradual and structural type system is helpful while mostly staying out of your way.
  • There’s good async support and thanks to Deno we get to use top-level async without any export { } hacks to convince Node we’re dealing with an ESM module.
  • The Deno namespace includes many useful helpers for things like argument parsing, dealing with environment variables, a test runner, and much more.
  • Additionally, there’s a pretty complete standard library, that also works with other JS/TS runtimes like Node or Bun.
  • Deno follows a secure by default approach requiring explicit permissions for filesystem or network access. For example, here’s the shebang line for my most recent script which is allowed to read from (but not write to) the filesystem and make network requests:
    #!/usr/bin/env -S deno run --allow-read --allow-net
    
  • You can write self-contained scripts utilizing dependencies from JSR or NPM, without the need for package.json or node_modules. The packages will be downloaded to Deno’s global cache when the script first runs. Here’s an example:
    import chalk from "npm:chalk@5.3.0";
    
    console.log(chalk.blue("Hello, world!"));
    
    Note: this probably wasn’t the best example, as console.log has supported styling for a while now (though not all of the CSS properties work outside the browser):
    console.log("%cHello, world!", "color: blue");
    
  • Deno 2 comes with a lot of built-in tools for formatting, linting, writing and running tests and benchmarks, calculating code coverage, etc. This makes for a development experience similar to Go. There’s even a tool for compiling (and cross-compiling), aptly called deno compile.

I could go on, but this list has gotten quite long already. Maybe this post will inspire you to give the TS+Deno combination a try and if you do I hope it works as well for you as it does for me.

Read the original on citizen428.net

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.