Skip to content

TypeScript fails to detect self-reference issue in "Parameter Properties" of constructors #62414

Open

Description

@rwalle

🔎 Search Terms

  • private property self reference
  • private property self reference constructor
  • Parameter Properties constructor self reference

🕗 Version & Regression Information

  • This is reproducible at least between v4.9.5 and nightly -- I have not tried earlier versions

⏯ Playground Link

https://www.typescriptlang.org/play/?target=9&module=1&ts=5.9.2#code/JYWwDg9gTgLgBAbzgKwsAdnAhgZzgfQDM4BfOQqCEOAcnQgBMBTALjCxgAsaBuAKD5YARjhhQsAY3gSANrjwBZAJ4BhOThwAhXE0QC4BuAHojcLsDwB3YDJlwJWAK45dFnI9eYA5gAoAlACEfIb2EOiiUI5S0HA+YFDAAG4cuoQsZkpgTBDERHAAvOR+eiEk+oZesQB0NVhQXjjpERheANoAusUIwSGGUEwwjlCY5jhVhD41VXUNfvylAj0GRgBUxqajcAwQTHj08JbQANYANHCcEJZMiUxQS3AgSgBijpg+aRlZOQTEhYRd9xCEzm9xIKyMfDKAlk8jgADkmJY1LCmAAPGBMdAMRSqdRaHQlQyoDDVWr1RpwZroNqdQm9MycCxVXxTGY4EELKESMKiewFODoRHwxHIjT+fjc8IQGRMKoyCC+CRVYnoHw0LA0M40ISa2gSGh+OZwIA

💻 Code

import { join as _f } from 'node:path';

abstract class MyClassBase {

    // this will cause issue in g()!
    constructor (private f: typeof _f = f) {
    }

    g (...args: string[]) {
        return this.f(...args);
    }


    /* // this does not work, however
    myFun (f: typeof _f = f) {
        f();
    }*/
}


class NewClass extends MyClassBase {
    join (...args: string[]) {
        this.g(...args);
    }
}

const c = new NewClass();
console.log(c.join('a', 'b', 'c')); 

🙁 Actual behavior

TypeScript compiler does not report any errors.

if you copy transpiled JS file and run that with Node.js, you will see

  constructor(f = f) {
                  ^

ReferenceError: Cannot access 'f' before initialization

🙂 Expected behavior

TypeScript clearly shows an error, similar to what would have been seen in regular function parameters

Parameter 'f' cannot reference itself.

Additional information about the issue

The issue originated from this -- microsoft/vscode-js-debug-browsers#19, and I just discovered that the issue in the project or TypeScript has never been fixed. If someone at Microsoft could do a favor and ping the maintainer of that repo as well, it will be much appreciated.

Activity

  1. mag123c commented on Sep 9, 2025

    @mag123c

    The root cause is that parameter property self-reference checks are missing in the type checker.
    I'll create a draft PR with a fix that adds detection in checkParameter for parameter properties with initializers.

  2. mag123c commented on Sep 10, 2025

    @mag123c

    I really want to try and help fix this issue.

    After studying the code more, I think the problem might be that parameter properties have two symbols (parameter + property), and the existing self-reference check might need to handle both.

    Would it be the right direction to extend the existing check in onSuccessfullyResolvedSymbol instead of adding a new check in checkParameter?

    I apologize for my previous inadequate PR. I'm eager to learn and contribute properly. Any guidance would be greatly appreciated.

    Thank you for your patience with a first-time contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions