firebase-functions icon indicating copy to clipboard operation
firebase-functions copied to clipboard

Make DocumentBuilder handlers into generic types

Open morgs32 opened this issue 3 years ago • 7 comments

So that we can type our functions better!

Description

Code sample

morgs32 avatar May 30 '21 20:05 morgs32

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

google-cla[bot] avatar May 30 '21 20:05 google-cla[bot]

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

googlebot avatar May 30 '21 20:05 googlebot

@googlebot I signed it!

morgs32 avatar May 30 '21 20:05 morgs32

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

google-cla[bot] avatar May 30 '21 20:05 google-cla[bot]

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

googlebot avatar May 30 '21 20:05 googlebot

Hey @morgs32. Thanks for taking the time to contribute.

While reviewing the code, I noticed that both DocumentSnapshot and QuerySnapshot types already default to DocumentData:

https://github.com/googleapis/nodejs-firestore/blob/28d645bd3e368abde592bfa2611de3378ca175a6/types/firestore.d.ts#L1121

https://github.com/googleapis/nodejs-firestore/blob/28d645bd3e368abde592bfa2611de3378ca175a6/types/firestore.d.ts#L1488

At least in VSCode, the typescript language server seems to make use of this type annotation:

Screen Shot 2021-06-01 at 8 08 47 PM

I'm pretty new to Typescript, and I'm unsure what features are unlocked when we make the proposed changes. Do you mind helping me understand by sharing an example or two?

taeold avatar Jun 02 '21 03:06 taeold

This would enable something like this - in which I can easily type the snapshot of the onWrite. Let me know if this makes sense @taeold

export const onWriteUserFirestoreHandler: onWrite<IUser> = async (props) => {

  const {
    after,
    // before
  } = props

  const userAfter = after.data()

  if (!userAfter) {
    return
  }

  // const userId = userAfter.id;

}

morgs32 avatar Jan 07 '22 16:01 morgs32