Skip to content

Instantly share code, notes, and snippets.

@faustienf
faustienf / opaque.ts
Created March 22, 2022 13:07
Opaque type in TypeScript
declare const opaque: unique symbol;
type Opaque<T, OpaqueType> = T & { readonly [opaque]: OpaqueType };
// examples
type Timestamp = Opaque<number, 'Timestamp'>;
const timestamp: Timestamp = Math.random() // Type 'number' is not assignable to type 'Timestamp'
const createQueue = () => {
let queue = Promise.resolve();
return <T extends () => any>(task: T) => {
queue = queue.then(() => task());
};
};
const queue = createQueue();
// example
#!/bin/bash
ssh-keygen -t ed25519 -f ~/.ssh/id_faustienf
touch ~/.ssh/config
echo "
Host faustienf.github.com
HostName github.com
User faustienf
IdentityFile ~/.ssh/id_faustienf
// Unility types begin
type Split<
S extends string,
Delimiter extends string
> = S extends `${infer Head}${Delimiter}${infer Tail}`
? [Head, ...Split<Tail, Delimiter>]
: S extends Delimiter
? []
: [S];
module.exports = {
create: function (context) {
const checkSpreadStatement = (node) => {
if (!['ObjectExpression', 'ArrayExpression'].includes(node.type)) {
return;
}
const properties = node.properties || node.elements;
properties.forEach((property) => {