Description
🔎 Search Terms
Searched GH Issues with is:issue state:open expected expression, got '.' error
🕗 Version & Regression Information
Tested on Nightly (5.9.0-dev.20250706) and range from 5.8.3 to 3.3.3. The error is reproducible on all versions.
⏯ Playground Link
💻 Code
// Type Setup
interface Foo {
bar?: Bar[],
}
interface Bar {
name: string,
}
// Variable Setup
const CONSTANT = "Some Value";
const foo: Foo = {
bar: [
{
name: "example value"
}
]
};
// Problematic syntax
const calculatedValue = foo
.bar?
.find(b => b.name.toLowerCase() === CONSTANT.toLowerCase());
// Working syntax of same constructs
// const calculatedValue = foo
// .bar
// ?.find(b => b.name.toLowerCase() === CONSTANT.toLowerCase());
console.log(calculatedValue === undefined); // true
🙁 Actual behavior
Multiple syntax errors are shown in IDE. Runtime error expected expression, got '.'
is thrown.
🙂 Expected behavior
Code compiles as expected and outputs true
.
Additional information about the issue
TypeScript is considered a free-form syntax language. As such, whitespace, indentation and breaks SHOULD NOT make an effect on runtime. This is clearly not the case here, where a given (otherwise) identical syntax (outside of whitespace, indentation and breaks) determines the runtime output.
It may be possible that the parser incorrectly determines ?
as a ternary operator, instead of an optional chaining operator. This example (without the types) can be replicated with JavaScript, so it is probable this is a generic JS/TS parser issue.