'TypeScript(Nodejs error) Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string

I have a nodejs App and am using Typescript for it and have implemented Classes and interfaces for the Models for Db . I have a model User class with interface too .I simply want to send notification and am using Puhser basic code like this

  
  let pusher = new Pusher({
    appId: process.env.PUSHER_APP_ID,
    key: process.env.PUSHER_APP_KEY,
    secret: process.env.PUSHER_APP_SECRET,
    cluster: process.env.PUSHER_APP_CLUSTER
    });
  pusher.trigger('notifications', 'user_added', user, req.headers['x-socket-id']);

I thought i would be simple but its giving the following error on all the fields like appId,key etc

(property) appId: string | undefined Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.ts(2322) index.d.ts(45, 5): The expected type comes from property 'appId' which is declared here on type 'Options'

i tried using pusher variable as interface but pusher is thirdparty system i tried

let pusher = new Pusher({
                        const appId: string = process.env.PUSHER_APP_ID,
                        const key: string = process.env.PUSHER_APP_KEY,
                        const secret: string = process.env.PUSHER_APP_SECRET,
                        const cluster: string = process.env.PUSHER_APP_CLUSTER
                    });
type here


Solution 1:[1]

You can cast the environment variables to string.

let pusher = new Pusher({
    appId: process.env.PUSHER_APP_ID as string,
    key: process.env.PUSHER_APP_KEY as string,
    secret: process.env.PUSHER_APP_SECRET as string,
    cluster: process.env.PUSHER_APP_CLUSTER as string,
});

Solution 2:[2]

becuase process.env.PUSHER_APP_ID value is undefined console.log(process.env.PUSHER_APP_ID) check value

Solution 3:[3]

The return type you get from process.env is going to be of type yourRequiredType | undefined because it could exist or not and that is what Typescript is trying to tell you.

You have two options:

  1. You can define the properties of your object to be the desired Type or undefined;

  2. You can implement a Typeguard to guarantee the correct type being passed in like so:

    const stringTypeGuard = (x: unknown): x is string => typeof x === string

before you pass your object to the db you can use this to guarantee your properties are what you want

Solution 4:[4]

Typescript will not know what variables will be defined in your environment at compile time so it will assume that process.env.{anything} may or may not be defined (i.e. it will be string|undefined if you know for certain that it will be defined then you can say:

  let pusher = new Pusher({
    appId: process.env.PUSHER_APP_ID!,
    key: process.env.PUSHER_APP_KEY!,
    secret: process.env.PUSHER_APP_SECRET!,
    cluster: process.env.PUSHER_APP_CLUSTER!
    });

the ! tells typescript that you know for certain that at the given point the variable will not be null or undefined

Solution 5:[5]

In my experience it's better to tupple your way out of it. Since TypeScript can't be sure that the env var is set, it can be either string or undefined. To remedy this, I always do it like this

let pusher = new Pusher({
    appId: process.env.PUSHER_APP_ID || "",
    key: process.env.PUSHER_APP_KEY || "",
    secret: process.env.PUSHER_APP_SECRET || "",
    cluster: process.env.PUSHER_APP_CLUSTER || ""
});

This will look at the env var, and if it's not set, it will simply make it "". This will guarantee that this always will be a string

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 stevejkang
Solution 2 Muhammad Nabeel
Solution 3 Jorge Guerreiro
Solution 4 apokryfos
Solution 5 Schiellerup