Skip to content

Add use-before-def error for uninitialized property#33248

Closed
rbuckton wants to merge 2 commits intomasterfrom
fix31223
Closed

Add use-before-def error for uninitialized property#33248
rbuckton wants to merge 2 commits intomasterfrom
fix31223

Conversation

@rbuckton
Copy link
Copy Markdown
Contributor

@rbuckton rbuckton commented Sep 4, 2019

Reports an error when referencing an uninitialized instance property from the initializer of another instance property when in --strictNullChecks and --strictPropertyInitialization.

Fixes #31223

computed = this.num * 10;
~~~
!!! error TS2729: Property 'num' is used before its initialization.
!!! related TS2728 tests/cases/compiler/useBeforeDeclaration_propertyDeclaration.ts:2:5: 'num' is declared here.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we tack on another related span to this message that says where num is initialized, since that's what the error is about?

Copy link
Copy Markdown
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good - I have a comment on the related span we're reporting (it seems to point at a not-so-useful location), but that's probably an existing issue (should we open a bug for it?).

@sandersn sandersn added the For Milestone Bug PRs that fix a bug with a specific milestone label Feb 1, 2020
@sandersn
Copy link
Copy Markdown
Member

@rbuckton is this ready to merge after it's brought up to date with master?

@rbuckton
Copy link
Copy Markdown
Contributor Author

rbuckton commented May 8, 2020

The failing test has an interesting case:

    class A {
        a: number;
        b = this.a;  // Error
                 ~
!!! error TS2729: Property 'a' is used before its initialization.
!!! related TS2728 tests/cases/compiler/initializerWithThisPropertyAccess.ts:2:5: 'a' is declared here.
        c = () => this.a;
        d = (new A()).a;
                      ~
!!! error TS2729: Property 'a' is used before its initialization.
!!! related TS2728 tests/cases/compiler/initializerWithThisPropertyAccess.ts:2:5: 'a' is declared here.
        constructor() {
            this.a = 1;
        }
    }

This first case of b = this.a is correctly an error. What's not clear is whether the second case of d = (new A()).a should be an error. Perhaps I need to restrict the error to this references (or references whose type is this)? Or should it stand?

# Conflicts:
#	src/compiler/checker.ts
@rbuckton
Copy link
Copy Markdown
Contributor Author

rbuckton commented May 8, 2020

I'm accepting the error as is for now since its still essentially true (its initialization is circular, so no 'a' will ever be initialized when trying to initialize d...).

@rbuckton
Copy link
Copy Markdown
Contributor Author

rbuckton commented Jan 4, 2021

This was superceded by #38030

@rbuckton rbuckton closed this Jan 4, 2021
@rbuckton rbuckton deleted the fix31223 branch January 4, 2021 22:33
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: Team For Milestone Bug PRs that fix a bug with a specific milestone

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

TypeScript allows class properties to reference instance variables that have not been defined yet.

5 participants