Created
February 10, 2025 19:07
-
-
Save JacobWeisenburger/0f6115a06a06af87bda5661190c2cb89 to your computer and use it in GitHub Desktop.
A way to make custom error messages for types
This file contains hidden or 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
type ValidateType<T extends string> = | |
T extends 'bad' ? 'Error message' | |
: T extends 'another bad' ? 'Another error message' | |
: T extends 'Error message' | 'Another error message' ? `Can't use '${ T }'` | |
: T | |
function fn<T extends string> ( arg: ValidateType<T> ) { } | |
fn( 'this should work' ) // works | |
fn( 'bad' ) // Argument of type '"bad"' is not assignable to parameter of type '"Error message"'. | |
fn( 'another bad' ) // Argument of type '"another bad"' is not assignable to parameter of type '"Another error message"'. | |
fn( 'Error message' ) // Argument of type '"Error message"' is not assignable to parameter of type '"Can't use 'Error message'"'. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment