Share
Embed
T3 Stack and TypeScript ecosystem
Yosuke Kurami
January 18, 2023
Transcript
20 slides
-
About me - Twitter / GitHub: @Quramy - Web Developer
(Node.js / Ruby on Rails ͷׂ߹͕ߴΊ) - Techfeed Expert (TypeScript) -
T3 Stack - 2022 ʹެ։͞Εͨ Web ΞϓϦέʔγϣϯ։ൃελοΫ - Backend
/ Frontend ํΛ TypeScript Ͱهड़͢ΔલఏͷϥΠϒϥϦߏ - https://github.com/t3-oss/create-t3-app -
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 -
Typesafe ͱʁ - "Typesafe" ͲͷΑ͏ͳঢ়ଶΛࢦ͢ͷͩΖ͏͔ - ʮ҆શੑʯ͕ίϯςΩετʹΑͬͯఆٛͷҟͳΔ༻ޠ - e.g. نఆ͞Εͨશͯͷܕʹ͍ͭͯະఆٛಈ࡞͕ଘࡏ͠ͳ͍͜ͱΛ
Typesafe ͱݺͿͳΒɺ JavaScript ܕʹ͍ͭͯ҆શͳݴޠ -
Typesafe ͱʁ - Question: ࣍ͷ TypeScript ίʔυ TypesafeͰ͔͢ʁ - TypeScript
Ͱ͋ͬͯ҆શͱݶΒͳ͍ - API Ϩεϙϯεͷܕ͕ any, fetch URL ύϥϝʔλ༩ํ๏ - զʑʹͱͬͯͷ Typesafe : ΞϓϦέʔγϣϯʹޡΓ͕ࠞೖ͢ΔՄೳੑ͕੩తʹۃখԽ͞Ε͍ͯΔঢ়ଶ const product = await fetch(`/api/products/1234`).then(res => res.json()) -
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 ΑΓ࠶ܝ -
How T3 tackle unknown Browser JS Node.js Internet Rendering Engine
Database tRPC Prisma TSX (React) -
tRPC - TypeScript ʹΑΔ Remote Procedure Call Λ࣮ݱ͢ΔϥΠϒϥϦ - ϑϧελοΫ
TypeScript ։ൃʹಛԽ GraphQL Open API ͕ݴޠඇґଘͰ͋Δʹ͓͍ͯɺੑ࣭͕ҟͳΔ - Zod ΤίγεςϜͷҰͭ - T3 ( Next.js ) Ͱ͏߹: react-query ΛϥϯλΠϜʹ༻͍Δ -
export const ProductDetail = () => { const { data,
isFetching } = trpc.productDetail.useQuery({ id: "1234" }) if (isFetching || !data) return <div>Loading...</div> return ( <div> Product name: {data.name} </div> ) } Browser JS Node.js const getContext = () => ({ prisma }) const { router, procedure } = initTRPC.context<typeof getContext>().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 -
tRPC ʹ͓͚Δ Typesafe - ϓϩγʔδϟ (= ؔ) ͷίʔυ͔ΒΫϥΠΞϯτܕఆٛΛਪ͢Δ Zod ෦
DSL ͱͯ͠ར༻͞Ε͍ͯΔ - Mapped types / Conditional types Λ༻͍ͨܕਪ https://github.com/trpc/trpc/blob/b18b8134eb666e2c88fe3c245ebf78e7c9817859/packages/server/src/ core/types.ts#L53-L66 -
Prisma - 2019 ࠒʹొͨ͠ TypeScript ( Node.js ) ༻ͷ ORM
※ લͰ͋ΔGraphQLϕʔεͷ Prisma v1 ͱผͱଊ͑ͯΑ͍ - Typesafe ͳΫΤϦͷهड़͕Մೳ - ֤छRDB ( MySQL / PostgreSQL / SQLite / SQLServer) ʹରԠ - ϚΠάϨʔγϣϯ༻ͷ CLI ଐ͢Δ - ϥϯλΠϜͰಈ࡞͢ΔΫΤϦΤϯδϯ Rust -
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 -
Prisma ͱ TypeScript - ֎෦DSL (Schema) ͔ΒɺTypeScript ιʔείʔυΛࣗಈੜ͢Δઓུ - ͜ͷҙຯͰɺGraphQL
Open API ͷΤίγεςϜʹ͍ۙ͠ - ίʔυੜʹ CLI ࣮ߦ͕ඞཁͱͳΔ͕ɺੜࣗମಡΈ͍͢ - tRPC Zod ͷ߹, ਪաఔͷܕใ͕ෳࡶͰܕύζϧΈ͕ڧ͍ -
·ͱΊ - T3 Axiom Ͱ͋Δ "Typesafety is not optional" Λେࣄʹ͠·͠ΐ͏
- Typesafety ΛಘΔ͜ͱ ≒ unknown Λ͢͜ͱ - TypeScript ܕਪύλʔϯ: Zod, tRPC, react-hook-form, etc... - .ts ίʔυࣗಈੜύλʔϯ: Prisma, Relay, graphql-codegen, etc... -
͓·͚ - TypeScript ϑϨϯυϦͳελοΫ T3 Ҏલʹ৭ʑ͋Δ͕ɺͲ͜ʹ৽ نੑ͕͋Δͷ͔ - tRPC ؚΊɺZod
Λத৺ʹਾ͑ͨΤίγεςϜ͕৳ͼ͍ͯΔ https://2022.stateofjs.com/en-US/other-tools/