admin管理员组

文章数量:1335624

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 es 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

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 es 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
Share Improve this question asked Mar 22, 2022 at 8:36 Syed MurtazaSyed Murtaza 412 silver badges4 bronze badges 4
  • Is PUSHER_APP_ID (etc..) defined in the process' enviroment? – ewokx Commented Mar 22, 2022 at 8:39
  • Make a sure your environment variables are set. – Shahnawaz Kadari Commented Mar 22, 2022 at 8:39
  • the return type you get from process.env will be string or undefined because it could either exist or not so you have two options, you either define the types you pass to your DB as string | undefined or you implement a typeguard to guarantee the correct type being passed in – Jorge Guerreiro Commented Mar 22, 2022 at 8:40
  • Yes all the variables are stored in Env File – Syed Murtaza Commented Mar 22, 2022 at 8:52
Add a ment  | 

5 Answers 5

Reset to default 4

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

TypeScript does not know what variables will be defined in your environment at pile 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 non-null assertion operator ! tells TypeScript that you know for certain that at the given point the variable will not be null or undefined.

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

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

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

本文标签: