SlideShare a Scribd company logo
1
PracticeTypeScript
Techniques Building React
ServerComponentsApp
Maurice de Beijer
@mauricedb
 Maurice de Beijer
 The Problem Solver
 Microsoft MVP
 Freelance developer/instructor
 Currently at https://someday.com/
 Twitter: @mauricedb
 Web: http://www.TheProblemSolver.nl
 E-mail: maurice.de.beijer@gmail.com
3
© ABL - The Problem Solver
Topics
 Compiling the code to catch type errors early
 Using the satisfies operator
 Use type mappings to optimize your code
 Custom type mappings are really powerful
 Can help detect all sorts of errors quickly
 Can make types more readable in IntelliSense
 Use Template LiteralTypes to manipulate types
 Use OpaqueTypes to help with type safety
 MakeTypeScript even stricter and catch more potential errors
 Prevent objects from being mutated by accident
 Improve performance when importing large JSON files
© ABL - The Problem Solver 4
Type it out
by hand?
“Typing it drills it into your brain much better than
simply copying and pasting it.You're forming new
neuron pathways.Those pathways are going to
help you in the future. Help them out now!”
© ABL - The Problem Solver 5
Prerequisites
Install Node & NPM
Install the GitHub repository
© ABL - The Problem Solver 6
Install
Node.js & NPM
© ABL - The Problem Solver 7
Following
Along
 Repo: https://github.com/mauricedb/ts-conf-2023-ws
 Slides: https://bit.ly/ts-conf-2023-ws
© ABL - The Problem Solver 8
Create a new
Next.js app
© ABL - The Problem Solver 9
AddingShadcn
components
© ABL - The Problem Solver 10
The changes
© ABL - The Problem Solver 11
Clone the
GitHub
Repository
© ABL - The Problem Solver 12
Install NPM
Packages
© ABL - The Problem Solver 13
Start branch
© ABL - The Problem Solver 14
 Start with the 00-start branch
 git checkout --track origin/00-start
Start the
application
© ABL - The Problem Solver 15
The
application
© ABL - The Problem Solver 16
Compiling the code
© ABL - The Problem Solver 17
Compiling the
code
 Quite often TypeScript code is not type checked during
development
 Create React App use Babel (JavaScript)
 Vite uses ESBuild (Go)
 Next.js uses SWC (Rust)
 These removeTypeScript annotations and treat the code as
modern JavaScript
 This means thatTypeScript type errors can go unnoticed 
 Run the TypeScript compiler to check the code
© ABL - The Problem Solver 18
package.json
© ABL - The Problem Solver 19
npm run compile
© ABL - The Problem Solver 20
shopping-
cart.tsx
© ABL - The Problem Solver 21
npm run compile
© ABL - The Problem Solver 22
Compile andTest
onGitHub
© ABL - The Problem Solver 23
Compile and
Test onGitHub
 Make sure to check your code with each pull request on GitHub
 Include ESLint and Next build
 Generate types from other sources if appropriate
 Prisma, GraphQL, OpenAPI etc.
 When included in GitHub they can be out of date 
© ABL - The Problem Solver 24
compile-and-test.yml
© ABL - The Problem Solver 25
What are ReactServer
Components?
© ABL - The Problem Solver 26
ReactServer
Components
 React Server Components only execute on the server
 Traditionally React components always execute in the browser
 This is not the same as Server Side Rendering
 With SSR components are executed both on the client and server
 Applications are a combination of server and client components
 The result is that the back and front-end code are more integrated
 Leading to better type safety 
© ABL - The Problem Solver 27
Before RSC
© ABL - The Problem Solver 28
With RSC
© ABL - The Problem Solver 29
ReactServer
Components
© ABL - The Problem Solver 30
 Server components can be asynchronous
 Great to load data from some API
 Server components render just once
 No re-rendering with state changes or event handling
 The server component code is not send to the browser
 Can safely use secure API key’s etc.
 Smaller bundle sizes
 ☞ React Server Components requireTypeScript 5.1 ☜
Client
Component
© ABL - The Problem Solver 31
 Server components can render server and client components
 Client components can only render client components
 Adding 'use client’ to the top of a component makes it a
client component
Server
Component
© ABL - The Problem Solver 32
Satisfies operator
© ABL - The Problem Solver 33
Satisfies
operator
 The satisfies operator lets us validate that the type of an
expression matches some type, without changing the resulting
type of that expression.
 💡The goal is to report type error where they are caused 💡
© ABL - The Problem Solver 34
movie-card.tsx
© ABL - The Problem Solver 35
movies/page.tsx
© ABL - The Problem Solver 36
PreventOverfetching
© ABL - The Problem Solver 37
Prevent
Overfetching
 Using satisfies Prisma.MovieSelect is not optimal
 Allows Overfetching of movie data
 Making the application slightly slower than required
 This can be prevented with a type mapping
 Create a MovieSelect like object with just the required keys
© ABL - The Problem Solver 38
movies/page.tsx
© ABL - The Problem Solver 39
movies/page.tsx
© ABL - The Problem Solver 40
Break time
Photo by Mindspace Studio on Unsplash
Custom type mapping
© ABL - The Problem Solver 42
Custom type
mapping
 TheTypeScript type system itself is a programming language
 With variables, loops, conditional logic etc.
 Example: turn Zod schema into it’sTypeScript type
© ABL - The Problem Solver 43
Type mapping
Boolean logic
© ABL - The Problem Solver 44
type-mapping.ts
© ABL - The Problem Solver 45
The display of types
© ABL - The Problem Solver 46
The display of
types
 Type mappings sometimes lead to hard to read types
 Easy to fix with another type mapping
© ABL - The Problem Solver 47
type-mapping.ts
© ABL - The Problem Solver 48
type-mapping.ts
© ABL - The Problem Solver 49
type-mapping.ts
© ABL - The Problem Solver 50
OpaqueTypes
© ABL - The Problem Solver 51
OpaqueTypes
 A lot of business data ultimately end up as a primitive data type
 They are all modeled as string, number etc.
 The compiler doesn’t know the difference between them
 A PO box number and invoice total amount are both type “number”
 The same for the compiler
 Very different for the business case
 Opaque types can make it easier to reason about code
 By providing distinct types and a clear separation
Console
output
© ABL - The Problem Solver 53
checkout-
shopping-
cart.ts
© ABL - The Problem Solver 54
checkout-
shopping-
cart.ts
© ABL - The Problem Solver 55
checkout-
dialog.tsx
© ABL - The Problem Solver 56
More strict features
© ABL - The Problem Solver 57
MoreStrict
Features
 There are many more strict settings not enabled by “strict”
 allowUnreachableCode
 allowUnusedLabels
 exactOptionalPropertyTypes
 noFallthroughCasesInSwitch
 noImplicitOverride
 noImplicitReturns
 noPropertyAccessFromIndexSignature
 noUncheckedIndexedAccess
 noUnusedLocals
 noUnusedParameters
© ABL - The Problem Solver 58
noUnchecked
IndexedAccess
 By default every index from an array is seen as the array element type
 Even if it exceeds the items available and will result in undefined
 Enabling “noUncheckedIndexedAccess” requires you to check the
element before using
 Whether the element is the array element type or undefined
 Try showing the Horror movies and observe a runtime error 
© ABL - The Problem Solver 59
tsconfig.json
© ABL - The Problem Solver 60
movies/page.tsx
© ABL - The Problem Solver 61
Using Readonly<>
© ABL - The Problem Solver 62
Readonly<T>
 The Readonly<T> mapped type creates a read-only mapped type
 Can’t change properties anymore
 Or use “array.push()” etc.
 ⚠️ Readonly<T> is not recursive ⚠️
 Only the first level of properties becomes read-only
 There is also a ReadonlyArray<T> mapped type
 💡Recommended for function arguments to show intent💡
 And AJAX responses etc.
© ABL - The Problem Solver 63
movie-card.tsx
© ABL - The Problem Solver 64
Custom type mapping
DeepReadonly<>
© ABL - The Problem Solver 65
DeepReadonly<T>
 Make a whole nested object structure read-only
 Recursive mapped types are very powerful
 An improvement over the default “Readonly<T>”
 Source:
https://gist.github.com/basarat/1c2923f91643a16a90de638e76bce0ab
© ABL - The Problem Solver 66
movies/page.tsx
© ABL - The Problem Solver 67
Template LiteralTypes
© ABL - The Problem Solver 68
Template
LiteralTypes
 Builds on string literal types but manipulates type definitions
 For example change a types snake case to camel case key names
© ABL - The Problem Solver 69
type-mapping.ts
© ABL - The Problem Solver 70
type-mapping.ts
© ABL - The Problem Solver 71
type-
mapping.ts
© ABL - The Problem Solver 72
TypingJSON files
© ABL - The Problem Solver 73
TypingJSON
files
 IntelliSense will parse an imported JSON file to determine the types
 This can be very slow with large files
 Add a <filename>.d.json.ts to explicitly type the imported data
 ⚠️Will not validate that the JSON actually has the correct shape ⚠️
© ABL - The Problem Solver 74
genres.d.json.ts
© ABL - The Problem Solver 75
tsconfig.json
© ABL - The Problem Solver 76
seed.ts
© ABL - The Problem Solver 77
Conclusion
 Compiling the code to catch type errors early
 Compile andTest your code with each pull request
 Using the satisfies operator
 Use type mappings to optimize your code
 Prevent over fetching of data with Prisma
 Custom type mappings are really powerful
 Can help detect all sorts of errors quickly
 Make mapped types more readable
 Using another type mapping 
 OpaqueTypes can help with type safety
 Not all strings represent the same data but forTypeScript a string is a string
 Use more strict features beyond the basics
 Using noUncheckedIndexedAccess can help catch potential runtime errors
 Using Readonly<> and ReadonlyArray<>
 Prevent objects from being mutated by accident
 Custom type mapping DeepReadonly<>
 Use Template Literal Types to manipulate types
 Typing JSON files can help performance when importing large JSON files
© ABL - The Problem Solver 78
Maurice de Beijer
@mauricedb
maurice.de.beijer
@gmail.com
© ABL - The Problem Solver 79

More Related Content

PPTX
Building Reliable Applications Using React, .NET & Azure
PPTX
Building reliable applications with React, C#, and Azure
PPTX
Building large and scalable mission critical applications with React
PPTX
Production-ready Next.js App with Cursor AI
PDF
TYPESCRIPT-ARCHI.pdfbsjjsjsjsjjsjjsjsjjs
PDF
BSides Vegas 2024_ Don’t Make This Mistake_ Painful Learnings of Applying AI ...
PPTX
The new React
PDF
Oh the compilers you'll build
Building Reliable Applications Using React, .NET & Azure
Building reliable applications with React, C#, and Azure
Building large and scalable mission critical applications with React
Production-ready Next.js App with Cursor AI
TYPESCRIPT-ARCHI.pdfbsjjsjsjsjjsjjsjsjjs
BSides Vegas 2024_ Don’t Make This Mistake_ Painful Learnings of Applying AI ...
The new React
Oh the compilers you'll build

Similar to Practice TypeScript Techniques Building React Server Components App (20)

PDF
TypeScript: Angular's Secret Weapon
PDF
Brief analysis of Media Portal 2 bugs
PPTX
C++ on the Web: Run your big 3D game in the browser
PDF
TDD - for people who don't need it
PDF
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
PDF
AppeX and JavaScript Support Enhancements in Cincom Smalltalk
PDF
The Ultimate Question of Programming, Refactoring, and Everything
PDF
The Ultimate Question of Programming, Refactoring, and Everything
PPT
Introduction to .NET
PDF
How to write good quality code
PDF
Write Generic Code with the Tooling API
PDF
Mobile Weekend Budapest presentation
PDF
TypeScript: Angular's Secret Weapon
PDF
2011-02-03 LA RubyConf Rails3 TDD Workshop
PPTX
Better React state management with Redux
PPTX
MWLUG 2015 - An Introduction to MVC
PDF
Errors detected in the Visual C++ 2012 libraries
PPTX
Build testable react app
PDF
SFScon 21 - Davide Montesin - Typescript vs. Java
TypeScript: Angular's Secret Weapon
Brief analysis of Media Portal 2 bugs
C++ on the Web: Run your big 3D game in the browser
TDD - for people who don't need it
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
AppeX and JavaScript Support Enhancements in Cincom Smalltalk
The Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and Everything
Introduction to .NET
How to write good quality code
Write Generic Code with the Tooling API
Mobile Weekend Budapest presentation
TypeScript: Angular's Secret Weapon
2011-02-03 LA RubyConf Rails3 TDD Workshop
Better React state management with Redux
MWLUG 2015 - An Introduction to MVC
Errors detected in the Visual C++ 2012 libraries
Build testable react app
SFScon 21 - Davide Montesin - Typescript vs. Java
Ad

More from Maurice De Beijer [MVP] (20)

PPTX
Full-stack App in half a Day: Next.js 15 Development Bootcamp
PPTX
Building Robust Web Applications with Test-Driven Development and Playwright:...
PDF
Mastering React Server Components and Server Actions in React 19
PPTX
A foolproof Way to Estimate a Software Project
PPTX
Surati Tech Talks 2022 / Build reliable Svelte applications using Cypress
PPTX
Build reliable Svelte applications using Cypress
PPTX
Building Reliable Applications Using React, .NET & Azure
PPTX
Concurrent Rendering Adventures in React 18
PPTX
Why I am hooked on the future of React
PPTX
Building reliable web applications using Cypress
PPTX
Getting started with React Suspense and concurrent rendering
PPTX
React suspense, not just for Alfred Hitchcock
PPTX
From zero to hero with the Reactive extensions for JavaScript
PPTX
Why I am hooked on the future of React
PPTX
From zero to hero with the reactive extensions for JavaScript
PPTX
Why I am hooked on the future of React
PPTX
I am hooked on React
PPTX
Create flexible React applications using GraphQL apis
PPTX
Create flexible react applications using GraphQL API's
PPTX
Docker for a .NET web developer
Full-stack App in half a Day: Next.js 15 Development Bootcamp
Building Robust Web Applications with Test-Driven Development and Playwright:...
Mastering React Server Components and Server Actions in React 19
A foolproof Way to Estimate a Software Project
Surati Tech Talks 2022 / Build reliable Svelte applications using Cypress
Build reliable Svelte applications using Cypress
Building Reliable Applications Using React, .NET & Azure
Concurrent Rendering Adventures in React 18
Why I am hooked on the future of React
Building reliable web applications using Cypress
Getting started with React Suspense and concurrent rendering
React suspense, not just for Alfred Hitchcock
From zero to hero with the Reactive extensions for JavaScript
Why I am hooked on the future of React
From zero to hero with the reactive extensions for JavaScript
Why I am hooked on the future of React
I am hooked on React
Create flexible React applications using GraphQL apis
Create flexible react applications using GraphQL API's
Docker for a .NET web developer
Ad

Recently uploaded (20)

PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
System and Network Administraation Chapter 3
PPT
Introduction Database Management System for Course Database
PPTX
Introduction to Artificial Intelligence
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
System and Network Administration Chapter 2
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Materi_Pemrograman_Komputer-Looping.pptx
PPTX
AIRLINE PRICE API | FLIGHT API COST |
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
VVF-Customer-Presentation2025-Ver1.9.pptx
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx
Odoo POS Development Services by CandidRoot Solutions
System and Network Administraation Chapter 3
Introduction Database Management System for Course Database
Introduction to Artificial Intelligence
2025 Textile ERP Trends: SAP, Odoo & Oracle
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
ISO 45001 Occupational Health and Safety Management System
System and Network Administration Chapter 2
How to Migrate SBCGlobal Email to Yahoo Easily
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
Softaken Excel to vCard Converter Software.pdf
Upgrade and Innovation Strategies for SAP ERP Customers
Materi_Pemrograman_Komputer-Looping.pptx
AIRLINE PRICE API | FLIGHT API COST |
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...

Practice TypeScript Techniques Building React Server Components App

Editor's Notes

  • #8: Vite requires Node.js version >=12.2.0
  • #19: Check out const notString: string = 1; in pages\index.tsx
  • #42: Photo by Mindspace Studio on Unsplash