Typescript optional chaining error: Expression expected.ts(1109)

Toma Radu-Petrescu

I am trying to do optional chaining in Typescript + React Native.

Let's say I have the following interfaces:

interface Bar {
  y: number
}

interface Foo {
  x?: Bar
}

and I try to run the following:

 const test: Foo = {x: {y: 3}};
 console.log(test.x?.y);

VSCode will show an error under the ?. saying the following: Expression expected.ts(1109)

Do you have any idea why this is happening or how should I fix it? Thanks.

wentjun

For those who are wondering, optional chaining (the ? operator) is now available on TypeScript 3.7 (Beta), as of October 2019. You may install that version by running the following command:

npm install [email protected]

This is how you may use the operator, as explained on the release notes.

let x = foo?.bar.baz();

Which is equivalent to

let x = (foo === null || foo === undefined) ?
    undefined :
    foo.bar.baz();

Apart from Optional Chaining, other interesting features include Nullish Coalescing (the ?? operator).

Update (November 2019)

TypeScript's optional chaining is now officially available. Installing the latest version of typescript should allow you to access the cool new features.

npm install typescript

For those who are using VS Code, please refer to Austin's excellent answer on getting it to work with the new TypeScript version.

For those who are working with WebStorm, you will need to configure TypeScript to use your project's installed TypeScript version.

Preferences -> Languages & Frameworks -> TypeScript

In addition, if you are using an older version of WebStorm, you may face an error/warning when you try to use the Nullish Coaslescing operator (??). To fix this, you will need to install WebStorm 2019.2.4, or any version that is newer than the above.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Optional chaining (?.) in nashorn

From Java

Webpack cant compile ts 3.7 (Optional Chaining, Nullish Coalescing)

From Java

How to use optional chaining with array in Typescript?

From Java

How to enable optional chaining with Create React App and TypeScript

From Dev

ContinueWith chaining not working as expected

From Dev

Error: expected primary expression

From Dev

Expected expression error with %

From Dev

swift optional chaining with cast

From Dev

Optional Chaining in JavaScript

From Dev

error: expected expression before ‘float’

From Dev

Optional Chaining or ternary expression in Swift?

From Dev

Optional Chaining Not Working As Expected

From Dev

Typedef in TypeScript error: 'expected variableDeclarator'

From Dev

Optional Chaining returning an Int

From Dev

error: expected expression before '{' token|

From Dev

Bash error: Integer expression expected

From Dev

Illegal start of expression and ';' expected error

From Dev

error: expected primary-expression before ‘int’

From Dev

javascript syntax error expected expression, got '<'

From Dev

Java8 Optional with Function chaining expression

From Dev

ts1109 : expression expected angular error

From Dev

Typescript compile error: error TS1109: Expression expected

From Dev

Char array error, expected an expression, error

From Dev

Swift 3.0 Optional Chaining

From Dev

Optional chaining with Swift strings

From Dev

Typescript error TS1005: ':' expected. with Object.assign()

From Dev

C++ lambdas, "error: expected expression"

From Dev

Chaining Optional.orElseThrow

From Dev

Object is possibly 'undefined' Error on Optional Chaining Typescript

Related Related

HotTag

Archive