Programming with Types
Examples in TypeScript
Vlad Riscutia
  • October 2019
  • ISBN 9781617296413
  • 336 pages
  • printed in black & white

Well-presented, valuable knowledge on type systems, data structures, algorithms, and programming paradigms (functional and OO).

Fred Heath, Faria Education Group

Programming with Types teaches you to design safe, resilient, correct software that’s easy to maintain and understand by taking advantage of the power of strong type systems. Designed to provide practical, instantly useful techniques for working developers, this clearly written tutorial introduces you to using type systems to support everyday programming tasks.

About the Technology

Common bugs often result from mismatched data types. By precisely naming and controlling which data are allowable in a calculation, a strong type system can eliminate whole classes of errors and ensure data integrity throughout an application. As a developer, skillfully using types in your everyday practice leads to better code and saves time tracking down tricky data-related errors.

About the book

Programming with Types teaches type-based techniques for writing software that’s safe, correct, easy to maintain, and practically self-documenting. Designed for working developers, this clearly written tutorial sticks with the practical benefits of type systems for everyday programming tasks. Following real-world examples coded in TypeScript, you’ll build your skills from primitive types up to more-advanced concepts like functors and monads.

Table of Contents detailed table of contents

1 Introduction to typing

1.1 Who This Book Is For

1.2 Why Types Exist

1.2.1 0s and 1s

1.2.2 What are Types and Type Systems?

1.3 Benefits of Type Systems

1.3.1 Correctness

1.3.2 Immutability

1.3.3 Encapsulation

1.3.4 Composability

1.3.5 Readability

1.4 Types of Type Systems

1.4.1 Dynamic and Static Typing

1.4.2 Weak and Strong Typing

1.4.3 Type Inference

1.5 In This Book


2 Basic types

2.1 Designing Functions That Don’t Return Values

2.1.1 The Empty Type

2.1.2 The Unit Type

2.1.3 Exercises

2.2 Boolean Logic and Short Circuits

2.2.1 Boolean Expressions

2.2.2 Short Circuit Evaluation

2.2.3 Exercise

2.3 Common Pitfalls of Numerical Types

2.3.1 Integer Types and Overflow

2.3.2 Floating-point Types and Rounding

2.3.3 Arbitrarily Large Numbers

2.3.4 Exercises

2.4 Encoding Text

2.4.1 Breaking Text

2.4.2 Encodings

2.4.3 Encoding Libraries

2.4.4 Exercises

2.5 Building Data Structures with Arrays and References

2.5.1 Fixed-Size Arrays

2.5.2 References

2.5.3 Efficient Lists

2.5.4 Binary Trees

2.5.5 Associative Arrays

2.5.6 Implementation Tradeoffs

2.5.7 Exercise


Answers to exercises

3 Composition

3.1 Compound Types

3.1.1 Tuples

3.1.2 Assigning Meaning

3.1.3 Maintaining Invariants

3.1.4 Exercise

3.2 Expressing either-or with types

3.2.1 Enumerations

3.2.2 Optional Types

3.2.3 Result or Error

3.2.4 Variants

3.2.5 Exercises

3.3 The visitor pattern

3.3.1 A Naïve Implementation

3.3.2 Using the Visitor Pattern

3.3.3 Visiting a Variant

3.3.4 Exercise

3.4 Algebraic data types

3.4.1 Product Types

3.4.2 Sum Types

3.4.3 Exercises


Answers to exercises

4 Type safety

4.1 Avoiding primitive obsession to prevent misinterpretation

4.1.1 The Mars Climate Orbiter

4.1.2 The Primitive Obsession Anti-Pattern

4.1.3 Exercise

4.2 Enforcing constraints

4.2.1 Enforcing Constraints with The Constructor

4.2.2 Enforcing Constraints with a Factory

4.2.3 Exercise

4.3 Adding Type Information

4.3.1 Type Casting

4.3.2 Tracking Types Outside the Type System

4.3.3 Common Typecasts

4.3.4 Exercises

4.4 Hiding and restoring type information

4.4.1 Heterogenous collections

4.4.2 Serialization

4.4.3 Exercises


Answers to exercises

5 Function types

5.1 A simple strategy pattern

5.1.1 A Functional Strategy

5.1.2 Typing Functions

5.1.3 Strategy Implementations

5.1.4 First-Class Functions

5.1.5 Exercises

5.2 A state machine without switch statements

5.2.1 Early Programming with Types

5.2.2 State Machines

5.2.3 State Machine Implementation Recap

5.2.4 Exercises

5.3 Avoiding expensive computation with lazy values

5.3.1 Lambdas

5.3.2 Exercise

5.4 Using map, filter, and reduce

5.4.1 Map

5.4.2 Filter

5.4.3 Reduce

5.4.4 Library Support

5.4.5 Exercises

5.5 Functional programming


Answers to exercises

6 Advanced applications of function types

6.1 A simple decorator pattern

6.1.1 A Functional Decorator

6.1.2 Decorator Implementations

6.1.3 Closures

6.1.4 Exercises

6.2 Implementing a counter

6.2.1 An Object-Oriented Counter

6.2.2 A Functional Counter

6.2.3 A Resumable Counter

6.2.4 Counter Implementations Recap

6.2.5 Exercises

6.3 Executing long-running operations asynchronously

6.3.1 Synchronous Execution

6.3.2 Asynchronous Execution: Callbacks

6.3.3 Asynchronous Execution Models

6.3.4 Asynchronous Functions Recap

6.3.5 Exercises

6.4 Simplifying asynchronous code

6.4.1 Chaining promises

6.4.2 Creating promises

6.4.3 More about promises

6.4.4 async/await

6.4.5 Clean asynchronous code recap

6.4.6 Exercises


Answers to exercises

7 Subtyping

7.1 Distinguishing between similar types in TypeScript

7.1.1 Structural and Nominal Subtyping Pros and Cons

7.1.2 Simulating Nominal Subtyping in TypeScript

7.1.3 Exercises

7.2 Assigning anything to, assigning to anything

7.2.1 Safe deserialization

7.2.2 Values for error cases

7.2.3 Top and bottom types recap

7.2.4 Exercises

7.3 Allowed substitutions

7.3.1 Subtyping and sum types

7.3.2 Subtyping and collections

7.3.3 Subtyping and function return types

7.3.4 Subtyping and Function argument types

7.3.5 Variance recap

7.3.6 Exercises


Answers to exercises

8 Elements of object-oriented programming

8.1 Defining contracts with interfaces

8.1.1 Exercises

8.2 Inheriting data and behavior

8.2.1 The Is-A Rule of Thumb

8.2.2 Modeling a Hierarchy

8.2.3 Parameterizing Behavior of Expressions

8.2.4 Exercises

8.3 Composing data and behavior

8.3.1 The Has-A Rule of Thumb

8.3.2 Composite Classes

8.3.3 Implementing the Adapter Pattern

8.3.4 Exercises

8.4 Extending data and behavior

8.4.1 Extending Behavior with Composition

8.4.2 Extending Behavior with Mix-ins

8.4.3 Mix-in in TypeScript

8.4.4 Exercise

8.5 Alternatives to purely object-oriented code

8.5.1 Sum Types

8.5.2 Functional Programming

8.5.3 Generic Programming


Answers to exercises

9 Generic data structures

9.1 Decoupling concerns

9.1.1 A reusable identity function

9.1.2 The optional type

9.1.3 Generic types

9.1.4 Exercises

9.2 Generic data layout

9.2.1 Generic data structures

9.2.2 What is a data structure?

9.2.3 Exercises

9.3 Traversing any data structure

9.3.1 Using iterators

9.3.2 Streamlining iteration code

9.3.3 Iterators recap

9.3.4 Exercises

9.4 Streaming data

9.4.1 Processing pipelines

9.4.2 Exercises


Answers to exercises

10 Generic algorithms and iterators

10.1 Better map, filter, reduce

10.1.1 Map

10.1.2 Filter

10.1.3 Reduce

10.1.4 Filter/Reduce Pipeline

10.1.5 Exercises

10.2 Common algorithms

10.2.1 Algorithms instead of loops

10.2.2 Implementing a fluent pipeline

10.2.3 Exercises

10.3 Constraining type parameters

10.3.1 Generic data structures with type constraints

10.3.2 Generic algorithms with type constraints

10.3.3 Exercise

10.4 Efficient reverse and other algorithms using iterators

10.4.1 Iterator building blocks

10.4.2 A Useful find

10.4.3 An Efficient reverse

10.4.4 Efficient element retrieval

10.4.5 Iterators recap

10.4.6 Exercises

10.5 Adaptive algorithms

10.5.1 Exercise


Answers to exercises

11 Higher kinded types and beyond

11.1 An even more general map

11.1.1 Processing results or propagating errors

11.1.2 Mix-and-match function application

11.1.3 Functors and higher kinded types

11.1.4 Functors for functions

11.1.5 Exercise

11.2 Monads

11.2.1 Result or error

11.2.2 Difference between map() and bind()

11.2.3 The Monad Pattern

11.2.4 The Continuation Monad

11.2.5 The List Monad

11.2.6 Other Monads

11.2.7 Exercise

11.3 Where to next?

11.3.1 Functional programming

11.3.2 Generic programming

11.3.3 Higher kinded types and category theory

11.3.4 Dependent types

11.3.5 Linear types


Answers to exercises


Appendix A: TypeScript installation and source code

Appendix B: TypeScript cheat sheet

What's inside

  • Building data structures with primitive types, arrays, and references
  • How types affect functions, inheritance, and composition
  • Object-oriented programming with types
  • Applying generics and higher-kinded types

About the reader

You’ll need experience with a mainstream programming language like TypeScript, Java, JavaScript, C#, or C++.

About the author

Vlad Riscutia is a principal software engineer at Microsoft. He has headed up several major software projects and mentors up-and-coming software engineers.

placing your order...

Don't refresh or navigate away from the page.
print book $34.99 $49.99 pBook + eBook + liveBook
Additional shipping charges may apply
Programming with Types (print book) added to cart
continue shopping
go to cart

eBook $27.99 $39.99 3 formats + liveBook
Programming with Types (eBook) added to cart
continue shopping
go to cart

Prices displayed in rupees will be charged in USD when you check out.

FREE domestic shipping on three or more pBooks