
Episode 42
042: Comparing TypeScript and Elm's Type Systems
TypeScript and Elm have very different type systems with different goals. We dive into the different features and the philosophy behind their different designs.
October 25, 20211h 8m
Audio is streamed directly from the publisher (cdn.simplecast.com) as published in their RSS feed. Play Podcasts does not host this file. Rights-holders can request removal through the copyright & takedown page.
Show Notes
- TypeScript and Elm have different goals
- Soundness is not a goal of the TypeScript type system
- TypeScript Design Goals (and non-goals)
- TypeScript's
anytype - Nominal vs structural typing
TypeScript's any vs. Elm's Debug.todo
TypeScript's any essentially "turns off" type checking in areas that any passes through.
In Elm:
- You can get a type that could be anything with
Debug.todo, but you can't build your app with--optimizeif it has Debug.todo's in it - You will still get contradictions between inconsistent uses of a type that could be anything (see this Ellie example)
This Ellie example (with compiler error as expected) and this TypeScript playground example (with no error) show the difference.
anycan not be used in places that takeneveranyvsunknownJSON.parsereturnsany, as do many core and published typingsio-tslets you validate JSON similar to JSON decoders in Elm- Definitely Typed (published type definitions for NPM packages)
- Definitely Typed search
noImplicitAny- TypeScript `strict mode in tsconfig
- Dillon's post TypeScript's Blind Spots
- JS semantics allow types that may not be intended (like adding a string
+object,'' + {} === '[object Object]') - Function parameters are inferred to be
anyregardless of implementation if they aren't given an explicit type - Type narrowing
- TypeScript has untagged unions (just called Unions) - in Elm, there are only tagged unions (called Custom Types)
- Undefined vs null
- TypeScript's Void type
- TypeScript doesn't have checked exceptions like Java (there is a discussion about this on GitHub) - Elm only has explicit errors as data that must be handled exhaustively like other data types
- Discriminated unions vs Elm custom types
- Literal types
- TypeScript allows number literal values, but arithemtic expressions return generic
numbervalues instead of literal types - Enums
- Branded types in TypeScript vs opaque types
- Elm Radio Opaque Types episode
- Switch statements are not exhaustive - you can add an eslint rule to check that (or the never trick, assert unreachable)
- Key of operator in TypeScript
- TypeScript's type system can do some cool things that Elm can't (for better and for worse)
- Prisma
- Prisma advanced TS meetup talks
- Tuples in TypeScript are just arrays and use narrowing - Tuple in Elm is a specific type
elm-ts-interop- TypeScript handbook on official site