Skip to content

Instantly share code, notes, and snippets.

View Gerrit0's full-sized avatar

Gerrit Birkeland Gerrit0

View GitHub Profile
@Gerrit0
Gerrit0 / type.tsx
Created June 9, 2024 16:24
TypeDoc type print width estimation
function aggregate<T>(arr: T[], fn: (item: T) => number) {
return arr.reduce((sum, it) => sum + fn(it), 0);
}
function aggregateWithJoiner<T>(arr: T[], fn: (item: T) => number, joiner: string) {
return arr.reduce((sum, it) => sum + fn(it), 0) + (arr.length - 1) * joiner.length;
}
// Used to check if we should split a union into multiple lines.
export function estimatePrintWidth(type: Type): number {
return type.visit({
// @ts-check
/**
* typedoc-plugin-not-exported
* TypeDoc plugin that forces inclusion of non-exported symbols (variables)
* Originally from https://github.com/TypeStrong/typedoc/issues/1474#issuecomment-766178261
* https://github.com/tomchen/typedoc-plugin-not-exported
* CC0
*/
const {
@Gerrit0
Gerrit0 / build.js
Last active May 6, 2023 23:23
A esbuild setup with optional watching + server with live reload
// @ts-check
const fs = require("fs");
const http = require("http");
const esbuild = require("esbuild");
const { join } = require("path");
// ===== Configuration =====
const port = parseInt(process.argv[process.argv.indexOf("--port") + 1]) || 8080;
@Gerrit0
Gerrit0 / different-subtype.md
Last active July 31, 2021 13:33
Different subtype error explanation

The different subtype error is usually caused by two mistakes:

  1. Providing a default value which might not be specific enough.
function broke<T extends string>(x: T = "error"): T { return x }
//                               ^^^^^^^^^^^^^^
// Type 'string' is not assignable to type 'T'.
//  'string' is assignable to the constraint of type 'T', but
//  'T' could be instantiated with a different subtype of
//  constraint 'string'.
import { ok as assert } from 'assert'
import { readFile, writeFile } from 'fs/promises'
import { getHighlighter, getTheme } from 'shiki'
// This is bad... but Shiki doesn't export it from the root.
import { Highlighter } from 'shiki/dist/highlighter'
import { TLang } from 'shiki-languages'
import { TTheme } from 'shiki-themes'
import { createElement, JSX } from 'preact'
//@ts-check
'use strict';
const fs = require('fs');
const ts = require('typescript');
const path = require('path');
const Module = require('module');
// These are declared as globals
const SKIP_MODULES = new Set([
// ==UserScript==
// @name Block GIF avatars
// @namespace https://gerritbirkeland.com/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://discord.com/*
// @grant none
// ==/UserScript==
@Gerrit0
Gerrit0 / README.md
Last active March 16, 2020 01:59
Powershell Template Replacement

replace.ps1

This is a script to replace placeholders like {name} in a Word document with their values as defined in an Excel spreadsheet.

Usage

  1. Save replace.ps1 below somewhere on your machine.
  2. Create a Word document with your message, for example:

Hello {Name},

@Gerrit0
Gerrit0 / typedoc-rambling.md
Created February 28, 2020 02:48
Notes while thinking about TypeDoc's design

There are really three kinds of converters in TypeDoc

  1. File
  2. Modules
  3. Library

Each of these may share individual node converters, but they are fundamentally different. File mode creates a globals map. Modules mode creates a map representing the structure of the package. Library mode creates a map describing the project from one or more entry points.

Furthermore, giving each mode its own converter makes it possible for modes which don't care about which nodes are actually used to create a definition to avoid including those implementation details in their documentation. For instance, a module which is re-exported with export * as ns from './f' in library mode should just be another namespace in the documentation, while modules mode should include it as a module, and create a reference.

@Gerrit0
Gerrit0 / events.ts
Last active June 29, 2020 03:44
Simple Event emitter - Dual licenced under MIT and Apache 2.0
/**
* Simple, type safe, event emitter class.
*
* @example
* ```ts
* const x = new EventEmitter<{ a: [string] }>()
* x.on('a', a => a.repeat(123)) // ok
* x.on('b', console.log) // error, 'b' is not assignable to 'a'
* const y = new EventEmitter<{ a: [string]; [k: string]: unknown[] }>()
* y.on('a', a => a.repeat(123)) // ok