Skip to content

Instantly share code, notes, and snippets.

Avatar
🌴
🌞

Pierre-Antoine Mills millsp

🌴
🌞
View GitHub Profile
View unionizeDeep.ts
type MarkUndefinable<A, B> = {
[K in keyof A]: A[K] extends O.Object
? MarkUndefinable<A[K], A.At<B, K>>
: K extends keyof B ? A[K] | undefined : A[K]
}
type UnionizeDeep<A extends object, B extends object> =
O.Merge<MarkUndefinable<A, B>, B, 'deep', M.BuiltIn, undefined>
type test = A.Compute<UnionizeDeep<{
@millsp
millsp / error.ts
Created Apr 23, 2021
Go-style functional type-safe error handling
View error.ts
const checkNumber = check((thing: unknown) => {
if (typeof thing === 'number') {
return thing;
}
return ko(new E.NOT_NUMBER(thing));
});
const main = check(() => {
const [number0, e0] = checkNumber(9);
@millsp
millsp / index.ts
Created Apr 14, 2021
Prisma UserArgs
View index.ts
import { PrismaClient, Prisma } from '@prisma/client'
async function main() {
const prisma = new PrismaClient()
// Define a type that includes the relation to `Post`
const userWithPosts = Prisma.validator<Prisma.UserArgs>()({
include: { posts: true }
})
View gist:90929394c1da30b33f8378884d9de3b0
import { PrismaClient, Prisma } from '@prisma/client'
async function main() {
const prisma = new PrismaClient()
// Define a type that includes the relation to `Post`
const userWithPosts = Prisma.validator<Prisma.UserArgs>()({
include: { posts: true }
})
View PrimsaPromise.ts
class PrismaUser<T> implements PrismaPromise<T> {
[prisma]: true;
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2> {
throw new Error("Method not implemented.");
}
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): Promise<T | TResult> {
throw new Error("Method not implemented.");
}
[Symbol.toStringTag]: string;
@millsp
millsp / N Tuple
Created Jan 17, 2021
Create a tuple of N size in TypeScript
View N Tuple
type Tuple<A, N extends number, T extends unknown[] = []> = {
0: Tuple<A, N, [...T, A]>
1: T
}[
T['length'] extends N
? 1
: 0
];
type test0 = Tuple<'coucou', 4>;
@millsp
millsp / path.ts
Last active Mar 26, 2021
Dotted paths with ts-toolbelt
View path.ts
import {
A,
F,
B,
O,
I,
} from 'ts-toolbelt';
type OnlyUserKeys<O> =
O extends L.List
@millsp
millsp / ts-dts-bundle.sh
Last active May 21, 2020
Create a single bundle of your TypeScript d.ts declaration files
View ts-dts-bundle.sh
#!/bin/bash
mkdir -p out
npx tsc src/index.ts -d --emitDeclarationOnly --module amd --outFile out/index.d.ts &&
echo "
declare module '<npm package name>' {
import main = require('index');
export = main;
View ts-toolbelt-demo.ts
import {Object, Tuple, Number} from 'ts-toolbelt'
// Merge two types together
type merge = Object.MergeUp<{name: string}, {age?: number}>
// Update the type of a field
type update = Object.Update<{age: string}, 'age', number>
// Make some fields optional
type optional = Object.Optional<{name: string, age: number}, 'age'>
View cloudSettings
{"lastUpload":"2020-08-16T14:05:01.885Z","extensionVersion":"v3.4.3"}