Typescript does not support annotations on the catch variable. There is a proposal to allow this but it is still being discussed (see here)
Your only solution is to use a type assertion or an extra variable
catch(_e){
let e:Error= _e;
result = e.message;
}
catch(e){
result = (e as Error).message;
}
Unfortunately this will work as well and is completely unchecked:
catch(e){
result = e.MessageUps;
}
Note
As you can read in the discussion on the proposal, in JS not everything that is thrown has to be an Error
instance, so beware of this assumption
Maybe tslint with no-unsafe-any
would help catch this.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…