GarfieldKlon
GarfieldKlon

Reputation: 12120

Typescript TypeError with es2022 but not with es2021

I'm trying to update the target property of the tsconfig.json file from es2015 to es2022. But I get an error when running the tests, which I think only use tsc without babel:

Chrome Headless 110.0.5481.177 (Mac OS 10.15.7) TypeError: Cannot read properties of undefined (reading 'getSomeValue')

The code causing this problem:

export class MyService {
    private xyz$ = this.otherService.getSomeValue().pipe(...

    constructor(private readonly otherService: SettingsService) {}
}

My guess the error is related to the new class fields features introduced in es2022? But I don't see why it should be a problem here? It's running fine with es2021.

I'm not sure if the tsconfig.json helps?

{
    "compileOnSave": false,
    "compilerOptions": {
        "baseUrl": "./",
        "outDir": "./dist/out-tsc",
        "sourceMap": true,
        "declaration": false,
        "downlevelIteration": true,
        "experimentalDecorators": true,
        "moduleResolution": "node",
        "importHelpers": true,
        "target": "es2022",
        "module": "es2022",
        "resolveJsonModule": true
    }
}

Upvotes: 3

Views: 1924

Answers (1)

TmTron
TmTron

Reputation: 19371

Here are 2 related issues:

As far as I understand the reasoning, accessing a constructor parameter property in a class property is expected to fail with use-before-init, but the typescript compiler currently does not emit this error.

class Test {
  // constructor that sets a field
  constructor(private param: Param){
  }

  // this is expected to fail - this.param is NOT assigned yet
  a = this.param.a;
}

Upvotes: 3

Related Questions