Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

303
Vistas
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
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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,
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

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

about 4 years ago · Juan Pablo Isaza Denunciar

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda