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

166
Vistas
unknown data type in typescript

I have below code to access secret manager

export interface ConfigData {
dbname:string;
dbuser:string;
}

I hvae json data in secret manager like

{
"dbname" : "dbname",
"dbuser" : "dbuser"
}

function

private static async fetchSecretData(){
     let config_key = 'projects/xx/xx/xx/versions/latest';
    
       const client = new SecretManagerServiceClient();
       const [version] = await client.accessSecretVersion({name:config_key})
       console.log(version.payload?.data )
       return version.payload?.data?.toString() as unknown as ConfigData;
 }

But I need to cast this as unknown as ConfigData,

I want to cast this ConfigData without making it unknown.

If there is no alternate to do this, then what is the best option to get the keys from this unknown object.

Thanks

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Two remarks:

  • toString() will return a string representation of whatever data is, so casting this to any other type is not going work.
  • It looks like SecretManagerServiceClient is a part of the Google Cloud Secret Manager API. You don't have to provide your own types for those as the @google-cloud/secret-manager package already comes with its own type definitions.
  • From from this package, I can tell that the payload is typed like this:
                /** Properties of a SecretPayload. */
                interface ISecretPayload {

                    /** SecretPayload data */
                    data?: (Uint8Array|string|null);
                }

To me it's not quite clear what this data is supposed to be. If it's a JSON string then you need to parse it with JSON.parse(version.payload.data) and then append as ConfigData to that, to let TypeScript know the shape of the output. If it's a Uint8Array you need to .toString it first before parsing it with JSON.parse.

So you would get something like:

private static async fetchSecretData() {
    let config_key = 'projects/xx/xx/xx/versions/latest';
    const client = new SecretManagerServiceClient();
    const [version] = await client.accessSecretVersion({name:config_key})
    if(version?.payload?.data) {
        return JSON.parse(version.payload.data) as ConfigData
    } else {
        return null;
    }
 }
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