Angular应用给Firebase添加MFA:手机号参数报错求助
This error pops up due to TypeScript's strict type checking—here's how to fix it:
Why the Error Happens
The enrolledFactors array expects items of type UpdateMultiFactorInfoRequest, a base type for all MFA factors. But phoneNumber is a property that only exists on the phone-specific subtype UpdatePhoneMultiFactorInfoRequest. Since TypeScript can't automatically infer you're targeting a phone-based MFA factor, it throws an error when you include phoneNumber in the object.
Note: The official documentation example is likely written in JavaScript (which doesn't enforce type checks), so it works there without extra steps.
Solutions
1. Use a Type Assertion
Explicitly tell TypeScript your object is a UpdatePhoneMultiFactorInfoRequest using the as keyword:
import * as admin from 'firebase-admin'; admin.auth().updateUser('123456789', { multiFactor: { enrolledFactors: [ { phoneNumber: '+16505550003', displayName: 'Spouse\'s phone', factorId: 'phone', } as admin.auth.UpdatePhoneMultiFactorInfoRequest, ], }, }) .then((userRecord) => { console.log(userRecord.multiFactor.enrolledFactors); }) .catch((error) => { console.log(error); });
2. Explicitly Type the Factor Object
Define the phone MFA factor as a variable with the correct type first, then add it to the array:
import * as admin from 'firebase-admin'; // Define the phone factor with the specific type const phoneMfaFactor: admin.auth.UpdatePhoneMultiFactorInfoRequest = { phoneNumber: '+16505550003', displayName: 'Spouse\'s phone', factorId: 'phone', }; admin.auth().updateUser('123456789', { multiFactor: { enrolledFactors: [phoneMfaFactor], }, }) .then((userRecord) => { console.log(userRecord.multiFactor.enrolledFactors); }) .catch((error) => { console.log(error); });
Both methods resolve the type error by clarifying to TypeScript that you're working with a phone-based MFA factor, which includes the phoneNumber property.
内容的提问来源于stack exchange,提问作者GrzesiekO

