How to dynamically assign a value to a class property in TypeScript

I would like to dynamically assign multiple MyClass properties in TypeScript, so there is no need to write multiple lines of code to set class properties. I wrote the following code:

interface IMy
{
    visible?: boolean;
    text?: string;
}

class MyClass
{
    element: JQuery;

    assing(o: IMy): void
    {
        for (let [key, value] of Object.entries(o))
        {
            if (typeof value !== "undefined")
                this[key] = value;
        }
    }

    get visible(): boolean
    {
        return this.element.is(":visible");
    }

    set visible(visible: boolean)
    {
        this.element.css("display", visible ? "" : "none");
    }

    get text(): string
    {
        return this.element.html();
    }

    set text(text: string)
    {
        this.element.html(text);
    }
}

let t = new MyClass();
t.assign({ visible: true });
//instead t.visible = true;

//or

t.assign({ text: "Something" });
//instead t.text = "something";

//or

t.assign({ text: "Something", visible: false });
//instead t.visible = true; 
//        t.text = "something";

but I have problems in the this[key] = value;error line :

The index signature of an object type is implicitly of type "any".

What do I need to change, or is my approach completely wrong?
It would also be a good solution to set the default properties in the constructor when the interface is a constructor parameter.

Edited:
I really liked the records, so I use this code:

//extensions.d.ts
interface Object
{
    entries<T>(o: any): [string, T][];
    entries(o: any): [string, any][];
}
//exntesion.js !!note js not ts
if (!Object.entries)
{
    Object.entries = function* entries(obj)
    {
        for (let key of Object.keys(obj))
        {
            yield [key, obj[key]];
        }
    };
}

Edited 2:

, .
, - .

interface IMy
{
    prop1?: boolean;
    prop2?: string;
    prop3?: string;
    prop4?: number;
}

class MyClass implements IMy
{
    prop1: boolean = false;
    prop2: string = "";
    prop3: string = "Something";
    prop4: number = 0;

    constructor(properties: IMy)
    {
        this.assing(properties);
    }

    assing(o: IMy): void
    {
        let that = (<any>this);
        for (let key in o)
        {
            if (o.hasOwnProperty(key))
            {
                let value = (<any>o)[key];
                if (typeof value !== "undefined" && typeof that[key] !== "undefined")
                    that[key] = value;
            }
        }
    }

}

let my1 = new MyClass({ prop1: true });
let my2 = new MyClass({ prop2: "SomeText", prop3: "Anything" });
//I set prop2 and prop3
let my3 = new MyClass({ prop4: 10 });    
//I set prop4 (fourth parameter)

console.log(my1, my2, my3);
+4
2

--noImplicitAny . , . , .

- key, for .. of .

, , .

, , ES6 ( .entries(), ES6 , .

:

SO answer - , any, :

(<any>this)[key] = value;

.entries() .

+5

TypeScript 2.1 keyof .

function test()
{
    const my = new MyClass();
    my.assign({ prop1: 3});
}


type MyClassPartial = {
    [P in keyof MyClass]?: MyClass[P];
};

class MyClass
{
    prop1: number;
    prop2: string;
    assign(props: MyClassPartial)
    {
        for (const key of Object.keys(props))
        {
            this[key] = props[key];
        }
    }
}

You get some intellisense

+1

Source: https://habr.com/ru/post/1650012/


All Articles