Home > Back-end >  TypeScript(Nodejs error) Type 'string | undefined' is not assignable to type 'string&
TypeScript(Nodejs error) Type 'string | undefined' is not assignable to type 'string&

Time:03-22

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

CodePudding user response:

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,
});

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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

  • Related