1

@DougStevenson published a nice cloud functions tutorial here, but his cloud function code snippet uses TypeScript rather than JavaScript.

How would one convert the following code to vanilla JavaScript?

interface ClaimsDocumentData extends admin.firestore.DocumentData {
    _lastCommitted?: admin.firestore.Timestamp
}
export const mirrorCustomClaims =
functions.firestore.document('user-claims/{uid}')
.onWrite(async (change, context) => {
    const beforeData: ClaimsDocumentData =
        change.before.data() || {}
    const afterData: ClaimsDocumentData =
        change.after.data() || {}
})

1 Answer 1

2

Just strip the type data out of it.

exports.mirrorCustomClaims =
functions.firestore.document('user-claims/{uid}')
.onWrite(async (change, context) => {
    const beforeData = change.before.data() || {}
    const afterData = change.after.data() || {}
})

You can also set up another project with TypeScript, add the code to it, compile it, and simply observe the transpiled JavaScript.

The rest of that sample is likely to give you even more problems. Strongly suggest adopting TypeScript, since all JavaScript is valid TypeScript, and you can adopt TypeScript features into your code at the rate you choose.

Sign up to request clarification or add additional context in comments.

3 Comments

Doug, thanks for your answer. Might you edit it to include the beginning code portion in the snippet (interface... part). If so, I can mark your answer as accepted.
There is no interface keyword in JavaScript, and there is no equivalent code, since all it's doing is declaring a type. I suggest actually setting the function up in TypeScript, compile it down to JavaScript, and see what it generated.
I just finished setting up a separate project using ts at your suggestion. I do see it now. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.