Last active
November 17, 2022 10:05
-
-
Save OliverJAsh/878720876b0640852964dbdece2549f9 to your computer and use it in GitHub Desktop.
TypeScript type hierarchy for `object`, primitives, `{}`, and `unknown`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
----------- primitive ---------- | |
object non-nullable primitive nullable | |
------------- {} -------------- | |
---------------- unknown ----------------- | |
https://www.oreilly.com/library/view/programming-typescript/9781492037644/ch04.html | |
https://gist.github.com/OliverJAsh/381cd397008309c4a95d8f9bd31adcd7 | |
*/ | |
declare let nonNullablePrimitive: string | boolean | number | bigint | symbol; | |
declare let nullable: null | undefined; | |
declare let primitive: typeof nonNullablePrimitive | typeof nullable; | |
declare let object: object; | |
declare let empty: {}; | |
declare let unknown: unknown; | |
// Begin tests | |
primitive = object | |
nonNullablePrimitive = object | |
object = nonNullablePrimitive | |
object = primitive | |
object = nullable | |
object = {} | |
object = () => {} | |
object = empty // no error due to structural typing? | |
empty = nonNullablePrimitive | |
empty = object | |
empty = primitive | |
empty = nullable | |
unknown = nonNullablePrimitive; | |
unknown = object; | |
unknown = primitive; | |
unknown = empty; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment