Check your domain
13 March 2024
Updated: 08 April 2024
Check your domain
Domain driven development with TypeScript
Overview
- TypeScript
- The Domain
- Some Problems
- Some tools
- Why do this?
What is TypeScript?
- Statically typed programming language
- Structural typing system
- OOP and FP
- Compiled to Javascript
- Not “Javascript with Types”
The Domain
- Manufacturing of Planks
- Sustainable sourcing initiative
- After a plank is cut to size it must pass QA before being shipped
Code: Defining the Model
Poke some holes
Code: What potential issues are there in our model
The Usual Solution
-
Lots of unit tests
-
Documentation
-
“Assume it is valid at this point in the code”
-
What happens if we delete a check somewhere?
-
What happens if the implementation changes?
Tests are a regression hazard. Documentation goes out of sync
A Different Solution
“Make illegal states unrepresentable” - Yaron Minsky
Some Tools
- Group related things
- String literal types
- String literal types
Code: Union Types, Template Literal Types
Impossible States
Is there anything we have overlooked?
What are our states?
passedQA=true
andshipped=false
passedQA=true
andshipped=true
passedQA=false
andshipped=false
passedQA=false
andshipped=true
Boolean states are exponential
Explicit States
- A product in QA
- A product that has completed QA
- Has been shipped
- Not yet shipped
Modeling the desired state
- Union types
What do we see?
- We actually notice that we have a missing state - what happens if QA does not pass?
Being 100% Sure
- Can our dimensions be negative?
- Need to validate this
Code: Option Type and Branded Type usage
Why do this?
- Interrogate the domain
- Clarify intent
- Reduces testing