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

198
Visualizações
Not possible to convert JS with ?? to TS

I'm trying to convert lot of JS files with double question marks to TypeScript using tsc.

But unfortunately tsc compiler doesn't understand ??. Example: this.x = typeof params.x == "string" ? this._processStringParam(params.x, "x") : params.x ?? 0;

It just throws error: imageElement.js:70:96 - error TS1109: Expression expected. this.x = typeof params.x == "string" ? this._processStringParam(params.x, "x") : params.x ?? 0;

I do not understand one thing. This double question-marks is the main reason to convert to TS. What the point for me to fix it in JS before tsc then?

How to convert such js files to TypeScript then?

tsconfig.json looks like this:

{
    "compilerOptions": {
        "checkJs": false,
        "module": "commonjs",
        "target": "ES5",
        "allowJs": true,
        "rootDir": "./",
        "baseUrl": "./",
        "outDir": "./build",
    "noStrictGenericChecks": true,
    "skipLibCheck": true,
    "strictFunctionTypes": false,
        "lib": [
            "es2015",
            "dom"
        ]
    },
    "exclude": [
        "./build/**"
    ]
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try it by changing the target to "ES6"

This is because the ?? or nullish coalescing operation is added after ES2020

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this configuration:

    "compilerOptions": {
    /* Language and Environment */
    "target": "ES5",                                     /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */

    /* Modules */
    "module": "commonjs",                                /* Specify what module code is generated. */
    "outDir": "dist",                                   /* Specify an output folder for all emitted files. */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */

    /* Type Checking */
    "strict": true,                                      /* Enable all strict type-checking options. */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  }

code ts:

console.log('coallescing nullish with undefined: ', undefined ?? 'compare with undefined');
console.log('coallescing nullish with null: ', null ?? 'compare with null');
console.log('coallescing nullish with valid data: ', 'with data' ?? 'compare with null or undefined');

transpiled code to js:

console.log('coallescing nullish with undefined: ', undefined !== null && undefined !== void 0 ? undefined : 'compare with undefined');
console.log('coallescing nullish with null: ', null !== null && null !== void 0 ? null : 'compare with null');
console.log('coallescing nullish with valid data: ', 'with data' !== null && 'with data' !== void 0 ? 'with data' : 'compare with null or undefined');

Results on console:

coallescing nullish with undefined:  compare with undefined
coallescing nullish with null:  compare with null
coallescing nullish with valid data:  with data

This js code has ES5 compatibility.

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