Typescript - Extending Error class

前端 未结 6 966
一向
一向 2020-11-27 03:07

I\'m trying to throw a custom error with my \"CustomError\" class name printed in the console instead of \"Error\", with no success:

class CustomError extend         


        
6条回答
  •  没有蜡笔的小新
    2020-11-27 03:34

    Try this...

    class CustomError extends Error { 
    
      constructor(message: string) {
        super(`Lorem "${message}" ipsum dolor.`)
      }
    
      get name() { return this.constructor.name }
    
    }
    
    throw new CustomError('foo')
    

提交回复
热议问题