import { utility} from StackOverflow

When you want a simple utility function that isn’t part of the standard library, what do you do? A. Find a library and use itB. Write the function and add it to an internally shared collectionC. Cut and paste from StackOverflow I’ve done all three in my twenty years of development, and I’m here to … Read moreimport { utility} from StackOverflow

yargs –do-things-right

Sometimes the obvious, default API is never what you want. That’s when you have to memorize the “do things right” flag. For yargs, the command-line argument parser for Node.js, specify options and subcommands like this: There. Next time I can cut and paste this instead of scouring the internet for how to make yargs do … Read moreyargs –do-things-right

Read a file with promises in TypeScript on Node

import * as fs from “fs”; import { promisify } from “util”; const content: Promise<string> = promisify(fs.readFile)(“path/to/the/file”, { encoding: “UTF-8” }) I am tired of looking that up over and over every time I want to read a file into a promise in Node using TypeScript, so here it is. Next time, bring me here, Google. The promisify call turns fs.readFile from a callback-based method to a promise-based one. promisify accepts … Read moreRead a file with promises in TypeScript on Node

Talk: TypeScript for Enterprise Developers

Jessica Kerr talks about some of the great things in TypeScript, like the flexible type systems and the possibility to test before compilation, but also things that make TypeScript painful. She shows how Node and npm work differently from the JVM (or CLR), and some of the surprises she hit learning this language. Watch it … Read moreTalk: TypeScript for Enterprise Developers

Do Things Right with npm install

Lately I’ve been wrestling with npm. Here are some rules I’ve learned: Use `npm ci` rather than `npm install` `npm ci` will bring down exactly the dependencies specified in package-lock.json. `npm install` does more than that; it also tries to update some libraries to a more recent version. Sometimes it updates URLs or nonsense in … Read moreDo Things Right with npm install

Understanding Promises in JavaScript (with yarn and Legos)

TL;DR: creating a Promise splits off another path of execution, and the Promise object represents the end of that path. Calling .then adds code to the end of that path. You can think of your program’s execution as following a piece of yarn. this video illustrates the difference between a synchronous program and the same program … Read moreUnderstanding Promises in JavaScript (with yarn and Legos)

Reference: Typescript asynchronous sleep that returns a promise

I want some code to execute after a delay. I want to do this with promises, in TypeScript, asynchronously. Apparently this is hard. Here is the spell: const sleepPlease: (number) => Promise<void> =     promisify( (a, b) => setTimeout(b, a)); const slow: Promise<string> =     sleepPlease(500).then(() => “yay finally”);I imported promisify from “util”. setTimeout is … Read moreReference: Typescript asynchronous sleep that returns a promise

Dictionary Objects in JavaScript and TypeScript

TL;DR: when using an object as a dictionary in TypeScript/ES6, iterate through it using `Object.keys()`. Coming from statically typed languages, I keep looking for a Map or Dict type in TypeScript (or JavaScript). People use objects for this, though. Objects have key-value pairs in them, and you can add them and delete them and declare … Read moreDictionary Objects in JavaScript and TypeScript