Skip to content

Incorrect expected expression, got '.' error #62003

Closed as not planned
Closed as not planned
@Supermarcel10

Description

@Supermarcel10

🔎 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

https://www.typescriptlang.org/play/?#code/PTAEBUE8AcFNQMqwC4FdoCgCWA7ZsAnAMwEMBjeAMQHtrQBvDUZ0AIxIIH4AuUAIQ4BtALoAaDAF8M2PIVIV+HBkxY4SAW1i8AzsgK4A5uKnSQoAGocsJVgBt4SNJjLUcu0AGEA8gDkE4AEEfcFAAXlAAIgRqTQsSW1RYCIBuDBc3ZFAiWl4aOnDGFjYOXkEVIuZCiuq1TV4I2AAPDWh7UAA3eMSI8oqpIuFJVNMwAAUCajtYdRJkLDJQbUg8EkaMM3T3MniyVFtZ2AATSwT4cOzqdbBmADp2LiuWG6JcQ4AKVjCAPjYb2tgbshqAAZagAd0IHhI2lgbwAlGFQuFvH5AsFASDwZDobC4XDhmlXFsdnsDscumcsrQVHcOCpOM9Xh9vr9-hjQRCCFCYfDEcjfP4guB2ViuTj4fjpIS3NR7DdbNQDG9trZdvt8OTTnzQKgcIdYC8cEd8UA

💻 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions