Skip to content

typescriptbob/nominal_types

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nominal_types

How to simulate nominal types in TypeScript

This is the repo for a talk I gave at Bloomberg in November 2019. You can step though the talk here

Overview

Consider the code in bug.ts

Can you see the problem?

The type alias to string means UserName and ErrorCode and string are all interchangable.

How would we do this in java?

Consider the code in classes.java

We can do a similar thing in C++ classes.cpp and C# classes.cs

What if we try it in TypeScript? classes.ts

No Error!

I can use Symbol and type intersections.

What are Type Intersections? type_intersection.ts

What are Symbols? symbol_use.ts

Using these techniques we have symbol.ts

We can make it simpler.

noclasses.ts

As you can see this is almost identical to our original code but is type safe.

About

How to simulate nominal types in TypeScript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published