Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!