Skip to content

set but no get on class... but using get on instance of class compiles.  #30852

Closed
@rmbar

Description

@rmbar

TypeScript Version: 3.3.3333

Search Terms:
typescript default get when only set present

namespace Controller
{
    export class MainController
    {
        private readonly _codeWorkflowState = new CodeWorkflowState();

        private handleInterpreterWorkerMessage(e: any) 
        {
            if(this._codeWorkflowState.hasNexToInterpret)
            {
                // bug? nextToInterpret accessor does not exist. No static error reported. newerCode's value is undefined (the value) at runtime.
                const newerCode: string = this._codeWorkflowState.nextToInterpret;
            }
        }
    }

    class CodeWorkflowState
    {
        private _nextToInterpret : string | null = null;

        set nextToInterpret(code: string)
        {
            this._nextToInterpret = code;
        }

        get hasNexToInterpret() : boolean
        {
            return this._nextToInterpret !== null;
        }
    }
}

Expected behavior:

Error: no 'nextToInterpret' property on class 'CodeWorkflowState'

OR

Error: default compiler supplied accessor 'nextToInterpret' has type undefined but assigning to type string.

Actual behavior:

Compiles fine. Variable of type string has value undefined at runtime.

Playground Link: https://www.typescriptlang.org/play/index.html#src=namespace%20Controller%0D%0A%7B%0D%0A%20%20%20%20export%20class%20MainController%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%2F**%0D%0A%20%20%20%20%20%20%20%20%20*%20Tracks%20the%20current%20state%20of%20code%20moving%20from%20editor%2C%20to%20interpreter%2C%20to%20renderer.%0D%0A%20%20%20%20%20%20%20%20%20*%2F%0D%0A%20%20%20%20%20%20%20%20private%20readonly%20_codeWorkflowState%20%3D%20new%20CodeWorkflowState()%3B%0D%0A%0D%0A%20%20%20%20%20%20%20%20private%20handleInterpreterWorkerMessage(e%3A%20any)%20%2F%2F%20any%20because%20from%20outside%20TypeScript%20so%20can't%20enforce%20type%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20if(this._codeWorkflowState.hasNexToInterpret)%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20bug%3F%20nextToInterpret%20accessor%20does%20not%20exist.%20No%20static%20error%20reported.%20newerCode's%20value%20is%20undefined%20at%20runtime.%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20const%20newerCode%3A%20string%20%3D%20this._codeWorkflowState.nextToInterpret%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20class%20CodeWorkflowState%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20private%20_nextToInterpret%20%3A%20string%20%7C%20null%20%3D%20null%3B%0D%0A%0D%0A%20%20%20%20%20%20%20%20set%20nextToInterpret(code%3A%20string)%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20this._nextToInterpret%20%3D%20code%3B%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20%20%20%20%20get%20hasNexToInterpret()%20%3A%20boolean%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20this._nextToInterpret%20!%3D%3D%20null%3B%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%7D

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions