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

TypeScript Beyond the Basics

By Ryan Chenkie

TypeScript Beyond the Basics

Developing a real-world full-stack app often involves tedious threading of data across multiple layers of the stack. This is particularly undesirable during prototyping phases where the main goal may be just to demonstrate an idea or design. It’s also risky when going to production since data inconsistencies between the layers can lead to bugs. We show one solution to this velocity and type-safety dilemma via Nexus combined with Prisma.

  • 860