TypeScript programs can have lies slip in when assuming accurate types from external systems, so handling lies at the boundaries of code is crucial for type-safety.
Validating incoming data at the boundaries of code is important to prevent lies and ensure expected types, and tools like zod can help handle changes and drift in external system types.
Deep dives
Understanding the potential lies in TypeScript
When using TypeScript with external systems, such as APIs or legacy systems, it's important to be aware of the potential lies that can slip into your code. These lies occur when you assume the types that are being provided to you are accurate, but they can drift or change over time. It's crucial to handle these lies at the boundaries of your code by using appropriate validation and type inference tools. One such tool is zod, which allows you to define validation schemas and extract inferred types. By keeping the types close to the boundaries and reducing the number of lies in your code, you can ensure a more type-safe and robust system.
The importance of validation at the boundaries
When working with external systems, such as APIs or CMS, it's crucial to validate the incoming data at the boundaries of your code. This validation helps ensure that the data conforms to the expected types and prevents lies from seeping into your system. By using tools like zod or creating your own validation logic, you can handle scenarios where the external system's types might drift or change. Keeping the validation close to the boundaries allows for more confidence in your code and reduces the risk of bugs due to mismatched or unexpected data.
Understanding the role of zod in type validation
Zod is a validation library that helps in handling lies within your code. With zod, you can define validation schemas using JavaScript, and then extract the inferred types from those schemas. This allows you to validate incoming data and ensure that it matches your expected types. By using zod and similar tools, you can reduce the occurrence of lies in your code and ensure better type safety and accuracy throughout your system.
Leveraging runtime validation for improved type safety
Runtime validation, such as that provided by zod, can play a crucial role in achieving better type safety. By validating data at runtime, you can catch potential lies and inconsistencies in types and ensure that your code behaves as expected. This type of validation is particularly useful when working with external systems or handling data that might change over time. By incorporating runtime validation into your development process, you can further enhance the accuracy and robustness of your codebase.
Sam and Ryan talk about seams in TypeScript programs where lies can sneak in. They chat about how API calls, form inputs, and URLs all relate to this problem, the similarity between drifting types and service mocks in testing, zod, and how type-safe languages like Elm eliminate this problem entirely.