Skip to content

Instantly share code, notes, and snippets.

@sangheestyle
Created January 16, 2019 17:37
Show Gist options
  • Save sangheestyle/e0b05dcb1cb7a52e71710eebc8827c23 to your computer and use it in GitHub Desktop.
Save sangheestyle/e0b05dcb1cb7a52e71710eebc8827c23 to your computer and use it in GitHub Desktop.
just ts typing experiement
import { any } from 'ramda';
class DbError extends Error {
constructor(message: string) {
super(message);
// Set the prototype explicitly.
Object.setPrototypeOf(this, DbError.prototype);
}
}
class CacheError extends Error {
constructor(message: string) {
super(message);
// Set the prototype explicitly.
Object.setPrototypeOf(this, CacheError.prototype);
}
}
class CertError extends Error {
constructor(message: string) {
super();
this.message = message;
}
}
type BadRequest = DbError | CacheError;
type Forbidden = CertError;
const badRequestTypes = [DbError, CacheError];
function isBadRequest(x: any): x is BadRequest {
return any(c => x instanceof c, badRequestTypes);
}
const dbError = new DbError('DbError');
const cacheError = new CacheError('CacheError');
const certError = new CertError('CertError');
console.log(dbError instanceof DbError);
console.log(cacheError instanceof CacheError);
console.log(certError instanceof DbError);
console.log(`isBadRequest: ${isBadRequest(dbError)}`);
console.log(`isBadRequest: ${isBadRequest(cacheError)}`);
// console.log(typeof certError);
// console.log("kk");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment