Skip to content

Instantly share code, notes, and snippets.

View millsp's full-sized avatar
๐ŸŒด
๐ŸŒž

pierre millsp

๐ŸŒด
๐ŸŒž
View GitHub Profile
@millsp
millsp / ts-dts-bundle.sh
Last active April 22, 2025 19:18
Create a single bundle of your TypeScript d.ts declaration files
#!/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;
@millsp
millsp / prisma-timing-extension.ts
Last active June 3, 2023 02:55
Example of a Prisma Client extensions that creates type-safe timing methods
import { PrismaClient, Prisma } from "@prisma/client"
type Operation =
| 'findFirst'
| 'findFirstOrThrow'
| 'findUnique'
| 'findUniqueOrThrow'
| 'findMany'
| 'create'
| 'createMany'
@millsp
millsp / path.ts
Last active July 10, 2022 06:45
Dotted paths with ts-toolbelt
import {
A,
F,
B,
O,
I,
} from 'ts-toolbelt';
type OnlyUserKeys<O> =
O extends L.List
import { PrismaClient } from ".prisma/client";
/*
model CommentOptionalProp {
id String @id @default(auto()) @map("_id") @db.ObjectId
country String?
content CommentContent?
}
import { PrismaClient } from ".prisma/client";
/**
model Comment {
id String @id @default(auto()) @map("_id") @db.ObjectId
country String?
content CommentContent?
contents CommentContent[]
}
import { PrismaClient } from ".prisma/client";
(async () => {
const prisma = new PrismaClient();
await prisma.user.create({
data: {
email: Date.now() + "",
},
});
@millsp
millsp / unionizeDeep.ts
Created May 17, 2021 17:43
Unionize Deep
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 April 23, 2021 14:59
Go-style functional type-safe error handling
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 April 14, 2021 08:47
Prisma UserArgs
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 }
})
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 }
})