Skip to content

Instantly share code, notes, and snippets.

@martinrue
martinrue / satisfies.ts
Created November 30, 2022 21:16
The new TypeScript `satisfies` operator.
/*
The satisfies operator, introduced in TS 4.9, enforces
a constraint on a value without changing the inferred
type. It's super useful when you want to keep the most
specific type that can be inferred.
*/
type Data = {
value: number | string;
};
@martinrue
martinrue / serve.js
Created March 18, 2021 16:49
Using esbuild's serve function for an SPA, equivalent to webpack's `devServer.historyApiFallback`.
const http = require("http");
const esbuild = require("esbuild");
const serve = async (servedir, listen) => {
// Start esbuild's local web server. Random port will be chosen by esbuild.
const { host, port } = await esbuild.serve({ servedir }, {});
// Create a second (proxy) server that will forward requests to esbuild.
const proxy = http.createServer((req, res) => {
// forwardRequest forwards an http request through to esbuid.