Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Error handling in TypeScript: an alternative approach
// Value object
class CityName {
static readonly MIN_LEN_NAME: number = 3
static readonly MAX_LEN_NAME: number = 100
private constructor(private _name: string) { }
// Factory method with validation rules
static create(name: string): [CityName, Error] {
if (name == null || name.trim().length < CityName.MIN_LEN_NAME || name.trim().length > CityName.MAX_LEN_NAME) {
return [
null,
new InvalidNameError(name)
];
}
// No validation errors
return [new CityName(name), null]
}
get name(): string { return this._name; }
}
// Validation error
class InvalidNameError extends Error {
constructor(name: string) {
super(`The name "${name}" is not valid!`)
}
}
// Input
const text = prompt('Type the city name: ');
// How to use
const [city, error] = CityName.create(text);
if (error != null) {
alert(`Error: ${error.message}`);
} else {
alert(`Welcome to ${city.name}`)
}

Test on Playground

This approach was used in a clean architecture example, available here

@kasvith

This comment has been minimized.

Copy link

@kasvith kasvith commented Dec 17, 2020

Your approach is similar to golang's default method :)

@pauloafpjunior

This comment has been minimized.

Copy link
Owner Author

@pauloafpjunior pauloafpjunior commented Jan 1, 2021

Your approach is similar to golang's default method :)

Yes it is!! :)

@uetoyo

This comment has been minimized.

Copy link

@uetoyo uetoyo commented Jan 14, 2021

Nice! The pros is that you dont have to define a custom Result class. The downside is that cannot chain errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment