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 May 21, 2020 23:46
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;
{"lastUpload":"2020-08-16T14:05:01.885Z","extensionVersion":"v3.4.3"}
@millsp
millsp / N Tuple
Created January 17, 2021 19:33
Create a tuple of N size in TypeScript
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>;
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;
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 }
})
@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 }
})
@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 / 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<{
import { PrismaClient } from ".prisma/client";
(async () => {
const prisma = new PrismaClient();
await prisma.user.create({
data: {
email: Date.now() + "",
},
});
import { PrismaClient } from ".prisma/client";
/**
model Comment {
id String @id @default(auto()) @map("_id") @db.ObjectId
country String?
content CommentContent?
contents CommentContent[]
}