How to get optional chaining working in TypeScript?

前端 未结 2 1248
旧时难觅i
旧时难觅i 2021-02-08 11:47

Looks like optional chaining has landed. Here\'s an example

What I can\'t figure out is how to get TS to compile it properly. I\'m not getting any syntax errors in my pr

2条回答
  •  遇见更好的自我
    2021-02-08 12:09

    The problem is you are targeting esnext this will tell the compiler to output all language features as is without any transpilation. Set the language to es2020 (or below) and ?. and ?? will get transpiled to compatible code:

    (async function () {
        let imageFileId = (await db.query(sql`select id from image_files where sha256=${sha256}`))[0]?.id;
    })()
    

    Playground Link

    There is no fine-grained control over which language features get transpiled and which don't do you have to pick a version as a whole unfortunately,

提交回复
热议问题