Created
January 16, 2019 17:37
-
-
Save sangheestyle/e0b05dcb1cb7a52e71710eebc8827c23 to your computer and use it in GitHub Desktop.
just ts typing experiement
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
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