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

114
Visualizações
Type checking JavaScript for object properties which change type

With this https://code.visualstudio.com/docs/nodejs/working-with-javascript#_type-checking-javascript enabled in Settings, there are some objects with valid type changes.

Examples:

let options = {
        logging: false,
    }    
if (process.env.DEBUG_LOG == "true") {
        options.logging = console.log // vscode error: is not assignable to boolean
    }    

or

 let pgconfig = {
        ssl: { rejectUnauthorized: false },
        max: 5,
    }
if (process.env.NODE_ENV == 'development') {
        //no SSL on localhost
        pgconfig.ssl = false // vscode error
    }

These type changes are valid for the functions using them.

How to make vscode understand that both work, without using

// @ts-ignore comment on the line before the error:

This is a .js file, not .ts. I tried

let options = {
        logging: <any> false,
    } 

or
let options = {
        logging: false as any,
    } 

but these don't work in JS.

More info: https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html (not saying how to handle type changes)

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

0

Since option literals are open-ended, you can trick the compiler into deriving an any type for a field by assigning to the field after object construction:

let options = {};
options.logging = false;

An alternative is to use JSDoc to add an @type tag, which lets you be a bit more strict than the any type:

/** @type {{logging: (boolean|function(...*))}} */
let options = {
    logging: false,
}

(I hope I got that syntax right, but I didn't test it. See also Types in the Closure Type System.)

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