Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
What is the solution when I have to use a property that is not typed?

I'm trying to slowly migrate to TypeScript because I consider it a better alternative than plain old JavaScript.

There might be a major drawback (that I realised now) with it - not all attributes are typed.

For example:

In my NodeJS project I want to use the npm package jwt-decode. It has this .d.ts file:

export class InvalidTokenError extends Error {}

export interface JwtDecodeOptions {
  header?: boolean;
}

export interface JwtHeader {
  type?: string;
  alg?: string;
}

export interface JwtPayload {
  iss?: string;
  sub?: string;
  aud?: string[] | string;
  exp?: number;
  nbf?: number;
  iat?: number;
  jti?: string;
}

export default function jwtDecode<T = unknown>(
  token: string,
  options?: JwtDecodeOptions
): T;

For this piece of code:

var decoded: JwtHeader & JwtPayload = jwtDecode(bearerToken);
console.log(decoded);

I get this object:

{
  exp: 1645637327,
  iat: 1645601327,
  jti: 'removed',
  iss: 'http://localhost:8080/auth/realms/supercatalog',
  aud: 'account',
  sub: 'removed',
  typ: 'Bearer',
  azp: 'restapi',
  session_state: '2434f33d-73c4-4f38-8c80-e92356380ffa',
  acr: '1',
  'allowed-origins': [ '' ],
  realm_access: {
    roles: [
      'app-elev',
      'offline_access',
      'uma_authorization',
      'default-roles-supercatalog'
    ]
  },
  resource_access: { restapi: { roles: [Array] }, account: { roles: [Array] } },
  scope: 'email profile',
  sid: 'removed',
  email_verified: false,
  preferred_username: 'elev'
}

I need to get access to resource_access -> restapi -> roles.

Now, the problem is that it is not typed and I can't access it. If I try to console.log(decoded.resource_access) it won't work.

So, my question is as follows:

How would you manage this situation? What's the solution?

I'm thinking of:

  1. creating another interface which implements JwtPayload but has more attributes that I need - I don't like this, it would be very time-consuming to do that each time, I think

  2. use decoded as a plain JS object and access anything I want easily - I also don't like this, it defeats the whole purpose of TS.

How can a situation like this be managed?

Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

// describe your custom fields
interface MyJwt extends JwtHeader, JwtPayload {
    realm_access: {
        roles: string[] // a union of known types would be more relevant
                        // provided that it is a fixed list
    }
}

// as a good practice don't use var, but const
// and if it appears that you have to change it, switch to let
const decoded = jwtDecode<MyJwt>(bearerToken);
// or :
const decoded: MyJwt = jwtDecode(bearerToken);

// decoded has the expected type
console.log(decoded.realm_access.roles);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda