GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — StrictPropertyInitialization does not understand calls to helpers · Issue #21132 · microsoft/TypeScript

StrictPropertyInitialization does not understand calls to helpers #21132

New issue
Copy link
New issue
Copy link
Closed
Closed
StrictPropertyInitialization does not understand calls to helpers#21132
Copy link
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed

Description

@berickson1
Brent Erickson (berickson1)
opened on Jan 10, 2018
Issue body actions

TypeScript Version: 2.7.0-dev.20180110

Code

export class Test {
    private _foo: string;
    constructor() {
        this._init();
    }

    private _init() {
        this._foo = 'test';
    }
}

https://github.com/berickson1/Playground/blob/master/strictPropertyInitError.ts

Expected behavior:
Code compiles with strict flags on

Actual behavior:
strictPropertyInitError.ts(4,13): error TS2564: Property '_foo' has no initializer and is not definitely assigned in the constructor.

Activity

  1. Ryan Cavanaugh (RyanCavanaugh)
    added
    Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
    on Jan 10, 2018
  2. mhegazy commented on Jan 10, 2018

    @mhegazy
    Mohamed Hegazy (mhegazy)
    on Jan 10, 2018
    Contributor
    More actions

    The compiler does not analyze all calls from the constructor. for these, use definite assignment operator !. see #20166

  3. RyanCavanaugh commented on Jan 10, 2018

    @RyanCavanaugh
    Ryan Cavanaugh (RyanCavanaugh)
    on Jan 10, 2018
    Member
    More actions

    See also long discussion of how this would be an expected effect #8476

  4. berickson1 commented on Jan 11, 2018

    @berickson1
    Brent Erickson (berickson1)
    on Jan 11, 2018
    Author
    More actions

    I understand that inheritance could make tracking calls from the constructor tricky if public methods were called.
    Has there been any thought regarding just tracking private method calls for variable initialization? We've done something similar to reset internal state of classes for unit testing. In this way we could do the following.

    export class Test {
        private _foo: string;
        constructor() {
            this._init(); //<- follow this chain
            this.myMethod();//<- don't follow this chain
        }
    
        private _init(override?: string) {
            this._foo = override || 'test';
        }
        
        public myMethod() {
            this._foo = 'bar'; // This isn't used to track strict property init
        }
    
        public resetState() {
            this._init('reset');
        }
    }
  5. typescript-bot commented on Jan 25, 2018

    @typescript-bot
    TypeScript Bot (typescript-bot)
    on Jan 25, 2018
    Contributor
    More actions

    Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

  6. TypeScript Bot (typescript-bot)
    closed this as completedon Jan 25, 2018
  7. microsoft
    locked and limited conversation to collaborators on Jul 3, 2018
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

    Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.