Exploring TypeScript and Node.js: A Journey Through TIL Insights
Written on
Chapter 1: Introduction to TIL Programming
In this inaugural episode, I delve into TypeScript string patterns and the nuances of reading WebAssembly (WASM) metadata using Node.js. Over the past few months, I have been on a blogging journey, striving to publish at least one new post each month. However, with the rapid evolution of content consumption following the rise of AI, I find it increasingly challenging to carve out time and maintain motivation. The demand for lengthy blog articles and detailed tutorials seems to be declining, at least from my perspective.
Interestingly, despite my dwindling enthusiasm for extensive writing, my engagement on X/Twitter continues to thrive. Through daily interactions, I frequently share bite-sized learnings encapsulated in "TIL..." tweets. This sparked an idea: why not convert these brief insights into a series of blog entries? Thus, we embark on this journey—a blend of tweet brevity and blog depth. Let's explore what I learned in February 2024!
Section 1.1: TIL: Defining String Patterns with TypeScript
This month, I discovered how to utilize template literals in TypeScript to define specific string patterns:
type MetamaskChainId = 0x${string};
// ❌ Error: Type "123" is not assignable to type '0x${string}'
const id: MetamaskChainId = "123";
// ✅
const id: MetamaskChainId = "0x123";
Template literal types, introduced in TypeScript 4.1 (I know, it has been around for a while, but I still enjoy uncovering established features! 😉), allow us to create more precise type definitions. In the example above, MetamaskChainId is a type that requires a string to start with "0x". While TypeScript performs checks at compile time, it’s crucial to remember that these validations don’t occur at runtime. This feature isn't a fail-safe solution for type safety, but it significantly enhances code descriptiveness and usage guidance—especially with an IDE that supports IntelliSense.
Subsection 1.1.1: Understanding Template Literal Types
Section 1.2: TIL: Accessing WASM Metadata in Node.js
This month, I stumbled upon a valuable technique: reading a canister's WebAssembly (WASM) module metadata section using Node.js. This insight proved beneficial, particularly in the context of Juno's new Serverless Function feature, where comprehending developer-added extensions to smart contracts is vital.
Accessing this metadata is quite simple:
const { readFileSync } = require('node:fs');
const wasmBuffer = readFileSync("/path/to/satellite.wasm");
const mod = new WebAssembly.Module(wasmBuffer);
const metadata =
WebAssembly.Module.customSections(mod, "icp:public juno:build");
const decoder = new TextDecoder();
const buildType = decoder.decode(metadata[0]);
console.log(buildType);
To retrieve the metadata from our WebAssembly (WASM), we inspect its custom sections. A WASM module consists of various sections, most of which conform to the WASM specification. However, developers can add custom sections that are ignored during standard validation. These custom sections enable the embedding of additional data within WASM modules. For example, the name custom section allows developers to assign names to all functions and locals within the module, similar to "symbols" in a native build.
In this context, we instantiate a WebAssembly.Module using a locally stored WASM file. We then extract the custom sections, focusing on the first one, as there is no additional metadata in this example.
Chapter 2: Video Insights on Programming
In the first episode of the video series, "TIL A Programming Thing," we explore the concept that Bash functions do not return values, shedding light on a common misconception among developers.
In the third episode of the series, "TIL A Programming Thing," we discuss MySQL’s database structure, highlighting how it maps databases and clarifying essential database management concepts.
This marks the beginning of a blog series that fuses the succinctness of "TIL" tweets with the depth of blog writing. I hope you found this a concise yet informative read (for those who still enjoy reading blogs! 😉).
Thank you for joining me on this journey! For more engaging coding content, feel free to follow me on Twitter/X. 👨💻