Slide 1

Slide 1 text

T3 Λࢧ͑ΔTypeScript by @Quramy #techfeed_experts_night 2023.1.18

Slide 2

Slide 2 text

T3 Λࢧ͑ΔTypeScript by @Quramy #techfeed_experts_night 2023.1.18 ͷΤίγεςϜ

Slide 3

Slide 3 text

About me - Twitter / GitHub: @Quramy - Web Developer (Node.js / Ruby on Rails ͷׂ߹͕ߴΊ) - Techfeed Expert (TypeScript)

Slide 4

Slide 4 text

T3 Stack - 2022 ೥ ʹެ։͞Εͨ Web ΞϓϦέʔγϣϯ։ൃελοΫ - Backend / Frontend ૒ํΛ TypeScript Ͱهड़͢ΔલఏͷϥΠϒϥϦߏ ੒ - https://github.com/t3-oss/create-t3-app

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

T3 Axiom: Typesafety is not optional The stated goal of Create T3 App is to provide the quickest way to start a new full-stack, typesafe web application. We take typesafety seriously in these parts as it improves our productivity and helps us ship fewer bugs. Any decision that compromises the typesafe nature of Create T3 App is a decision that should be made in a different project. https://create.t3.gg/en/introduction#typesafety-isnt-optional

Slide 8

Slide 8 text

Typesafe ͱ͸ʁ - "Typesafe" ͸ͲͷΑ͏ͳঢ়ଶΛࢦ͢ͷͩΖ͏͔ - ʮ҆શੑʯ͕ίϯςΩετʹΑͬͯఆٛͷҟͳΔ༻ޠ - e.g. نఆ͞Εͨશͯͷܕʹ͍ͭͯະఆٛಈ࡞͕ଘࡏ͠ͳ͍͜ͱΛ Typesafe ͱݺͿͳΒ͹ɺ JavaScript ͸ܕʹ͍ͭͯ҆શͳݴޠ

Slide 9

Slide 9 text

Typesafe ͱ͸ʁ - Question: ࣍ͷ TypeScript ίʔυ͸ TypesafeͰ͔͢ʁ - TypeScript Ͱ͋ͬͯ΋҆શͱ͸ݶΒͳ͍ - API Ϩεϙϯεͷܕ͕ any, fetch URL ΍ύϥϝʔλ෇༩ํ๏ - զʑʹͱͬͯͷ Typesafe : ΞϓϦέʔγϣϯʹޡΓ͕ࠞೖ͢ΔՄೳੑ͕੩తʹۃখԽ͞Ε͍ͯΔঢ়ଶ const product = await fetch(`/api/products/1234`).then(res => res.json())

Slide 10

Slide 10 text

The unknown - TypeScript͕༧ଌͣ͠Β͍ྖҬ - JavaScriptҎ֎ͷΤίγεςϜͱͷ࿈ܞ - e.g. API req/res, database access, CSS / HTML template, etc... - ೗ԿʹunknownΛݮΒ͔͕͢DX޲্ͷ伴 https://speakerdeck.com/quramy/extends-developer-experience?slide=13 ΑΓ࠶ܝ

Slide 11

Slide 11 text

How T3 tackle unknown Browser JS Node.js Internet Rendering Engine Database tRPC Prisma TSX (React)

Slide 12

Slide 12 text

tRPC - TypeScript ʹΑΔ Remote Procedure Call Λ࣮ݱ͢ΔϥΠϒϥϦ - ϑϧελοΫ TypeScript ։ൃʹಛԽ GraphQL ΍ Open API ͕ݴޠඇґଘͰ͋Δ఺ʹ͓͍ͯɺੑ࣭͕ҟͳΔ - Zod ΤίγεςϜͷҰͭ - T3 ( Next.js ) Ͱ࢖͏৔߹: react-query ΛϥϯλΠϜʹ༻͍Δ

Slide 13

Slide 13 text

export const ProductDetail = () => { const { data, isFetching } = trpc.productDetail.useQuery({ id: "1234" }) if (isFetching || !data) return
Loading...
return (
Product name: {data.name}
) } Browser JS Node.js const getContext = () => ({ prisma }) const { router, procedure } = initTRPC.context().create() const productDetail = procedure .input(z.object({ id: z.string() })) .query(async ({ ctx: { prisma }, input: { id } }) => { const result = await prisma.product.findUnique({ where: { id } }) return { id: result.id, name: result.name, price: result.price, } }) export const appRouter = router({ productDetail, }) export type AppRouter = typeof appRouter Internet Inference Inference

Slide 14

Slide 14 text

tRPC ʹ͓͚Δ Typesafe - ϓϩγʔδϟ (= ؔ਺) ͷίʔυ͔ΒΫϥΠΞϯτܕఆٛΛਪ࿦͢Δ Zod ΋಺෦ DSL ͱͯ͠ར༻͞Ε͍ͯΔ - Mapped types / Conditional types Λ༻͍ͨܕਪ࿦ https://github.com/trpc/trpc/blob/b18b8134eb666e2c88fe3c245ebf78e7c9817859/packages/server/src/ core/types.ts#L53-L66

Slide 15

Slide 15 text

Prisma - 2019 ೥ࠒʹొ৔ͨ͠ TypeScript ( Node.js ) ༻ͷ ORM ※ લ਎Ͱ͋ΔGraphQLϕʔεͷ Prisma v1 ͱ͸ผ෺ͱଊ͑ͯΑ͍ - Typesafe ͳΫΤϦͷهड़͕Մೳ - ֤छRDB ( MySQL / PostgreSQL / SQLite / SQLServer) ʹରԠ - ϚΠάϨʔγϣϯ༻ͷ CLI ΋෇ଐ͢Δ - ϥϯλΠϜͰಈ࡞͢ΔΫΤϦΤϯδϯ͸ Rust ੡

Slide 16

Slide 16 text

const productDetail = procedure .input(z.object({ id: z.string() })) .query(async ({ ctx: { prisma }, input: { id } }) => { const result = await prisma.product.findUnique({ where: { id } }) return { id: result.id, name: result.name, price: result.price, } }) Node.js Database generator client { provider = "prisma-client-js" } datasource db { provider = "postgresql" url = env("DATABASE_URL") } model Product { id String @id @default(cuid()) name String @unique price Int createdAt DateTime @default(now()) updatedAt DateTime @updatedAt } Generate Import schema.prisma (DSL) Prisma Client .js / .d.ts file

Slide 17

Slide 17 text

Prisma ͱ TypeScript - ֎෦DSL (Schema) ͔ΒɺTypeScript ιʔείʔυΛࣗಈੜ੒͢Δઓུ - ͜ͷҙຯͰ͸ɺGraphQL ΍ Open API ͷΤίγεςϜʹ͍ۙ͠ - ίʔυੜ੒ʹ CLI ࣮ߦ͕ඞཁͱͳΔ͕ɺੜ੒෺ࣗମ͸ಡΈ΍͍͢ - tRPC ΍ Zod ͷ৔߹, ਪ࿦աఔͷܕ৘ใ͕ෳࡶͰܕύζϧΈ͕ڧ͍

Slide 18

Slide 18 text

·ͱΊ

Slide 19

Slide 19 text

·ͱΊ - T3 Axiom Ͱ͋Δ "Typesafety is not optional" Λେࣄʹ͠·͠ΐ͏ - Typesafety ΛಘΔ͜ͱ ≒ unknown Λ౗͢͜ͱ - TypeScript ܕਪ࿦ύλʔϯ: Zod, tRPC, react-hook-form, etc... - .ts ίʔυࣗಈੜ੒ύλʔϯ: Prisma, Relay, graphql-codegen, etc...

Slide 20

Slide 20 text

͓·͚ - TypeScript ϑϨϯυϦͳελοΫ͸ T3 Ҏલʹ΋৭ʑ͋Δ͕ɺͲ͜ʹ৽ نੑ͕͋Δͷ͔ - tRPC ؚΊɺZod Λத৺ʹਾ͑ͨΤίγεςϜ͕৳ͼ͍ͯΔ https://2022.stateofjs.com/en-US/other-tools/