Skip to content

Instantly share code, notes, and snippets.

@etherealHero
Created June 21, 2024 13:34
Show Gist options
  • Select an option

  • Save etherealHero/b0fd9fca6028ae82247aa47cdb72a6ee to your computer and use it in GitHub Desktop.

Select an option

Save etherealHero/b0fd9fca6028ae82247aa47cdb72a6ee to your computer and use it in GitHub Desktop.

Revisions

  1. etherealHero created this gist Jun 21, 2024.
    19 changes: 19 additions & 0 deletions extendable-object.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    /**
    * @typedef {Object} MyObject
    * @property {string} knownProperty - Известное свойство
    */

    /**
    * @returns {MyObject & { [key: string]: any }}
    */
    function createObject() {
    const obj = {
    knownProperty: "initial value"
    };
    return obj;
    }

    const objFromFunction = createObject();
    objFromFunction.newKey = "value"; // Нет ошибки, добавляется новое свойство
    console.log(objFromFunction.knownProperty); // Подсказка для известного свойства работает
    console.log(objFromFunction.newKey); // Динамическое свойство тоже доступно