Typescript 如何在try-catch块中键入Firebase JS SDK错误?

Typescript 如何在try-catch块中键入Firebase JS SDK错误?,typescript,firebase,google-cloud-firestore,typescript-typings,Typescript,Firebase,Google Cloud Firestore,Typescript Typings,考虑到将文档保存到Firestore的功能,我尝试键入err对象 async function saveToFirestore(obj: SOME_OBJECT, collection: string, docId: string) { try { await firebase.firestore().collection(collection).doc(docId).set(obj); } catch(err: firebase.firestore.FirestoreErr

考虑到将文档保存到Firestore的功能,我尝试键入
err
对象

async function saveToFirestore(obj: SOME_OBJECT, collection: string, docId: string) {
  try {
    await firebase.firestore().collection(collection).doc(docId).set(obj);
  }
  catch(err: firebase.firestore.FirestoreError) {
    // THIS DOESN'T SEEM TO BE POSSIBLE
  }
}
也尝试使用
firebase.FirebaseError

我得到了这个错误:

我要寻找的是一种对来自Firebase的错误对象自动完成的方法。人们通常如何处理这个问题?是否应始终将
错误
对象键入为
任何
未知
?为什么?


更新

考虑到不可能直接键入
catch(err)
,我的目标是这样:

catch(err) {
  if (err instanceof firebase.FirebaseError) {  // <--- THIS DOES NOT WORK
    throw `something for the FirebaseError`
  }
  else {
    throw `something else for other error types`
  }
}
catch(错误){

if(err instanceof firebase.FirebaseError){/据我所知,您不能在
catch
子句上指定类型,因为这表明您只捕获了某种类型的错误,而这在该语言中是不可能的

获取类型化变量的常见解决方法是在
catch
块中立即声明局部变量:

catch(e) {
  const err: firebase.firestore.FirestoreError = e;
}
另请参见本主题讨论: