I want to use optional chaining in my Ionic project but I receive errors even though I use the correct notation.
// Get Array (might be empty) const array = await getArray(); return array?.;
My compiler throws 3 errors:
ERROR in path/to/file.ts:12:34 - error TS1109: Expression expected. [ng] 12 return array?.; [ng] ~ [ng] path/to/file.ts:12:34 - error TS1003: Identifier expected. [ng] 12 return array?.; [ng] ~ [ng] path/to/file.ts:12:34 - error TS1005: ':' expected. [ng] 12 return array?.; [ng] ~
Array item access with optional chaining You can use bracket notation for optional chaining on arrays:
const arr = ['a', 'b', 'c', 'd']; let arrayItem = arr?.;
My typescript-version just seemed to be too old (3.4.3). Optional chaining was introduced in 3.7 (https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html), so it makes sense that it does not work in my old version.
I just assumed that my project was relatively up to date but turns out it wasn't.