xcatliu / typescript-tutorial

TypeScript 入门教程
https://ts.xcatliu.com
10.44k stars 1.33k forks source link

这边是把es6的类用作类型去判定的意思不,有点不理解 #218

Closed coderDGJ closed 2 years ago

coderDGJ commented 2 years ago

class ApiError extends Error { code: number = 0; } class HttpError extends Error { statusCode: number = 200; }

function isApiError(error: Error) { if (typeof (error as ApiError).code === 'number') { return true; } return false; }