Skip to content

Interface Fields/Class Properties Mapping Bug #7305

@iam1me

Description

@iam1me

The following code demonstrates a bug between interface fields and the class properties that the TypeScript compiler accepts in their place. The IDisplayInfo interface below has a DisplayName field. The test1 class implements this interface via a setter property function. The test2 class implements this interface via a getter property function. An instance of each is instantiated and each instance gets and sets the DisplayName property. This code successfully compiles, though it will obviously error at run-time.

module myModule{
    export interface IDisplayInfo{
        DisplayName: string;
        }

    class test1 implements IDisplayInfo
    {
        private _displayName: string;

        set DisplayName(name: string) {
            this._displayName = name;
        }
    }

    class test2 implements IDisplayInfo{
        private _displayName: string;

        get DisplayName() {
            return this._displayName;
        }
    }

    var t1: test1 = new test1();
    var t2: test2 = new test2();

    t1.DisplayName = "hi";
    var dn = t1.DisplayName;

    t2.DisplayName = "goodbye";
    dn = t2.DisplayName;    
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions