2

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
4
  • Is PUSHER_APP_ID (etc..) defined in the process' enviroment? Commented Mar 22, 2022 at 8:39
  • Make a sure your environment variables are set. 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 Commented Mar 22, 2022 at 8:40
  • Yes all the variables are stored in Env File Commented Mar 22, 2022 at 8:52

5 Answers 5

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,
});
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

0

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

2 Comments

well i have saved these values in env file but as in strict mode the Vscode is throwing error so i havent started the server yet
try these let pusher = new Pusher({ appId: process.env.PUSHER_APP_ID as string, key: process.env.PUSHER_APP_KEY, secret: process.env.PUSHER_APP_SECRET, cluster: process.env.PUSHER_APP_CLUSTER }); or past below line before code //@ts-ignore
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.