Skip to content

Instantly share code, notes, and snippets.

@JacobWeisenburger
Created February 10, 2025 19:07
Show Gist options
  • Save JacobWeisenburger/0f6115a06a06af87bda5661190c2cb89 to your computer and use it in GitHub Desktop.
Save JacobWeisenburger/0f6115a06a06af87bda5661190c2cb89 to your computer and use it in GitHub Desktop.
A way to make custom error messages for types
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