How to fix Typescript error "Object is possibly 'undefined'"

I'm building a cloud function that will use the Stripe API to process payments. This is within a firebase project. When I run firebase deploy I get the error "Object is possible 'undefined'" const existingSource = customer.sources.data.filter( (s) => s.id === source).pop(); I'm not sure how to resolve this.

Here is my xxx.ts where getorCreateCustomer exists

/** Read the stripe customer ID from firestore, or create a new one if missing */
export const getOrCreateCustomer = async(uid: string) => {
    const user = await getUser(uid); 
    const customerId = user && user.stripeCustomerId; 

    //if missing customerId, create it
    if (!customerId) {
        return createCustomer(uid); 
    }
    else {
        return stripe.customers.retrieve(customerId); 
    }
}

Based on the definitions and contents of your functions, TypeScript is unable to infer the return type of getOrCreateCustomer. It is making the assumption that it could return undefined, and its strict mode is calling you out on the fact that you could be referencing a property on an undefined object, which would result in an error at runtime.

What you need to do is declare the return type to be something that can't possibly be undefined, and make sure the code in the body of the function is correct on that guarantee (otherwise you'll get a new error).

If you can't do that (but you really should do that), you might want to instead disable strict mode in your tsconfig.json file, since that is what's enforcing this level of correctness in your code.

I suggest the first option, even if you have to write more lines of code, as it's a better use of TypeScript's typing system.

How to fix typescript error? I cloned the application from the working git repository, launched and received the error: Property 'language' does not exist on type 'Readonly<{}> & Readonl

What @Doug mentioned, but also you could write your logic to make sure that every part of customer.sources.data is not undefined...

ie:

const { sources } = customer

if (sources) {
  const { data } = sources 
  if (data) {
     // then filter / etc etc ...
  }
}

From what I can see in devextreme, the devDependencies is "typescript": "^2.0.3", so I'm not entirely sure why it's not working in SPFx Is there anyway to gulp serve and ignore TS errors? Observed Behavior

7 months later, I figured out the best solution.

I simply wrapped the the contents of the firebase callable function in the following if/else statement. It's a bit redundant but it works.

if (!context.auth) {
    // Throwing an HttpsError so that the client gets the error details.
    throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
        'while authenticated.');
  }
  else{ ...copy function code here }

If you don't care about the authentication piece you can simply define the type of context as any.

(data, context:any)

Open the package.json file (the one enumerating the NPM packages) and check if the typings package is already present within the dependencies or devDependencies node, together with the script required to run it during the post-install phase within the script block. If they’re not here, add them so that your file should look like the following:

The problem is most likely due to the strictPropertyInitialization flag firstly introduced in TypeScript 2.7 (and beyond), which could cause these kind of errors: if you recently upgraded your TypeScript TSC executable, or Visual Studio 2017, you are most likely dealing with the consequences of this breaking change.

If the answer to the previous question is yes, then you just found the reason to the “duplicate identifier” error. Remove the lines that declare a class/ property/ function twice since you already have the reference to the external d.ts file. That’s it, the error is now gone and you can build the complete solution again.

How to fix this error: Remove circular references like in the example from any objects you want to convert into JSON. Unexpected token ; Related errors: Expected ), missing ) after argument list. The JavaScript interpreter expected something, but it wasn’t there. Typically caused by mismatched parentheses or brackets.

Comments
  • Please edit the question to show getOrCreateCustomer, since that's the thing that's returning the possibly undefined object.
  • @DougStevenson Sorry about that, I edited and added the file. Hope you can help.