TypeScript Beyond the Basics

Ryan Chenkie

@ryanchenkie

Developer Advocate at Prisma

Let's Talk About Type Safety

@ryanchenkie

@prisma

function add(num1, num2) {
  return num1 + num2;
}

add(1, "foo");

Let's Talk About Type Safety

@ryanchenkie

@prisma

function add(num1: number, num2: number) {
  return num1 + num2;
}

add(1, "foo"); // compilation error

@ryanchenkie

@prisma

"I have no idea what I'm looking at"

@ryanchenkie

@prisma

type IsValid<
  T extends Prisma.PrismaClient,
  U extends keyof T
> = RequiredMethods extends keyof T[U]
  ? T[U][RequiredMethods] extends (args?: any) => any
    ? 1
    : 0
  : 0

type RequiredMethods = 'create' | 'findUnique' | 'delete' | 'update'

type Filter<T extends Prisma.PrismaClient> = {
  [K in keyof T]-?: {
    1: K
    0: never
  }[IsValid<T, K>]
}[keyof T]

@ryanchenkie

@prisma

Beyond the Basics

Generics

Index Type Query and Lookup Types

Template Literal Types

DEMO

@ryanchenkie

@prisma

Thank You!

@ryanchenkie