Created
April 5, 2021 10:58
-
-
Save reverofevil/7dad117e86d2d22835147b4804e4950f to your computer and use it in GitHub Desktop.
Custom type error messages in TypeScript
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
declare const StaticErrorTag: unique symbol; | |
type StaticError<E extends string> = { [StaticErrorTag]: E } | |
type AAndBAreSame<T> = T extends {a: any} | |
? T extends {b: T["a"]} | |
? T | |
: StaticError<"Types of `a` and `b` differ"> | |
: StaticError<"No `a` field"> | |
const f = <T,>(t: T & AAndBAreSame<T>) => {}; | |
f({a: 5, b: 10}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment