Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. (SOLVED) Create Alias in TypeScript

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    (SOLVED) Create Alias in TypeScript

    This topic has been deleted. Only users with topic management privileges can see it.
    • U
      uwe72 last edited by uwe72

      Hallo, kann mir jemand diese funktionierende JS-Function in TypeScript zu übersetzen? Ich kriege es nicht hin:

      const idAlias = "test12345";
      const idOrigin = "deconz.1.Lights.2.on";
      
      function createAlias(idSrc, idDst, typeAlias) {
         if(!getObject(idDst)) {
            var obj = {};
            obj.type = 'state';
            obj.common = getObject(idSrc).common;
            if(typeAlias) obj.common.type = typeAlias;
            obj.common.alias = {};
            obj.common.alias.id = idSrc;
            setObject(idDst, obj);
         } //else log ("Alias schon vorhanden: " + idDst, 'warn'); 
      }
      
      createAlias(idOrigin, idAlias, 'boolean');
      

      DANKE !!!!

      155c62f9-0220-498c-8278-7733071e2b1d-image.png

      javascript.0 (4416) script.js.common.#RGB#.CreateAliase: TypeScript compilation failed: obj.type = 'state'; ^ ERROR: Property 'type' does not exist on type '{}'. obj.common = getObject(idSrc).common; ^ ERROR: Property 'common' does not exist on type '{}'. obj.common.type = typeAlias; ^ ERROR: Property 'common' does not exist on type '{}'. obj.common.alias = {}; ^ ERROR: Property 'common' does not exist on type '{}'. obj.common.alias.id = idSrc; ^ ERROR: Property 'common' does not exist on type '{}'. setObject(idDst, obj); ^ ERROR: Argument of type '{}' is not assignable to parameter of type '(Omit<StateObject, "acl" | "_id"> & { _id?: string; acl?: StateACL; }) | (Omit<InstanceObject, "acl" | "_id"> & { _id?: string; acl?: ObjectACL; }) | ... 10 more ... | (Omit<...> & { ...; })'. Type '{}' is not assignable to type 'Omit<OtherObject, "acl" | "_id"> & { _id?: string; acl?: ObjectACL; }'. Type '{}' is missing the following properties from type 'Omit<OtherObject, "acl" | "_id">': common, type, native
      
      1 Reply Last reply Reply Quote 0
      • C
        CruziX last edited by CruziX

        @uwe72

        const idAlias = "test12345";
        const idOrigin = "deconz.1.Lights.2.on";
        
        type TAlias = {
         id: string;
        };
        
        type TCommon = {
         type?: string;
         alias?: TAlias;
        }; 
        
        type TCustomObject ={
         type: string;
         common: TCommon;
        };
        function createAlias(idSrc: string, idDst: string, typeAlias: string) {
           if(!getObject(idDst)) {
              var obj: TCustomObject = {
         type: 'state',
         common: {
        type: '',
        alias: {
        id: ''
        }
        }
        };
              obj.common = getObject(idSrc).common;
              if(typeAlias) obj.common.type = typeAlias;
              obj.common.alias.id = idSrc;
              setObject(idDst, obj);
           } //else log ("Alias schon vorhanden: " + idDst, 'warn'); 
        }
        

        Kurz mitm Handy getippt, vielleicht hilfts ja

        U 1 Reply Last reply Reply Quote 0
        • U
          uwe72 @CruziX last edited by uwe72

          @cruzix bin dir so dankbar, dass Du mein Problem angeschaut hast!!

          Leider gibts noch irgendwie ein "Cast-Problem":

          const idAlias = "test12345";
          const idOrigin = "deconz.1.Lights.2.on";
          
          /*{
            "_id": "alias.0.Light.Device_1.WORKING",
            "type": "state",
            "common": {
              "alias": {
                "id": "admin.0.connected"
              },
              "name": "WORKING",
              "role": "indicator.working",
              "type": "boolean"
            },
            "native": {}
          } */     
          
          type TAlias = {
              id: string;
          };
          
          type TCommon = {
              type: string;
              alias: TAlias;
          }; 
          
          type TCustomObject ={
              type: string;
              common: TCommon;
          };
          
          function createAlias(idSrc: string, idDst: string, typeAlias: string) {
              if (!getObject(idDst)) {
                  var obj: TCustomObject = {
                      type: '',
                      common: {
                          type: '',
                          alias: {
                              id: ''
                          }
                      }
                  };
                  obj.type = 'state';
                  obj.common = getObject(idSrc).common;
                  if(typeAlias) obj.common.type = typeAlias;
                  obj.common.alias = {};
                  obj.common.alias.id = idSrc;
                  
                  setObject(idDst, obj);
             }
          }
          

          Den ersten Fehler gibts an dieser Stelle (Zeile 44):
          obj.common = getObject(idSrc).common;

          Type '{ [x: string]: any; [x: number]: any; }' is missing the following properties from type 'TCommon': type, alias(2739)
          (property) common: TCommon
          
          C 1 Reply Last reply Reply Quote 0
          • C
            CruziX @uwe72 last edited by CruziX

            @uwe72 ja, verstehe was das Problem ist, habe in meinem vorherigen Post den Common type mit optionalen Properties ergänzt und die zeile entfernt wo alias mit {} gesetzt wird. Sollte nun klappen

            U 1 Reply Last reply Reply Quote 0
            • U
              uwe72 @CruziX last edited by

              @cruzix

              Danke dir!! Das TypeScript passt nun, irgendwie akzeptiert die setObject Methode noch nicht den Parameter:

              const idAlias = "test12345";
              const idOrigin = "deconz.1.Lights.2.on";
              
              type TAlias = {
               id: string;
              };
              
              type TCommon = {
               type?: string;
               alias?: TAlias;
              }; 
              
              type TCustomObject ={
               type: string;
               common: TCommon;
              };
              
              function createAlias(idSrc: string, idDst: string, typeAlias: string) {
                 if(!getObject(idDst)) {
                    var obj: TCustomObject = {
                       type: 'state',
                      common: {
                      type: '',
                      alias: {
                          id: ''
                      }
                  }
                  };
                      obj.common = getObject(idSrc).common;
                      if(typeAlias) obj.common.type = typeAlias;
                      obj.common.alias.id = idSrc;
                      setObject(idDst, obj);
                  } 
              }
              
              //createAlias("deconz.1.Lights.6.on", "alias.0.rgb.Z005.on", "boolean");
              
              Argument of type 'TCustomObject' is not assignable to parameter of type '(Omit<StateObject, "acl" | "_id"> & { _id?: string; acl?: StateACL; }) | (Omit<InstanceObject, "acl" | "_id"> & { _id?: string; acl?: ObjectACL; }) | ... 10 more ... | (Omit<...> & { ...; })'.
                Type 'TCustomObject' is not assignable to type 'Omit<OtherObject, "acl" | "_id"> & { _id?: string; acl?: ObjectACL; }'.
                  Property 'native' is missing in type 'TCustomObject' but required in type 'Omit<OtherObject, "acl" | "_id">'.(2345)
              (local var) obj: TCustomObject
              
              U 1 Reply Last reply Reply Quote 0
              • U
                uwe72 @uwe72 last edited by uwe72

                Gibts wirklich niemand von den ioBroker-Profis wo dieses Thema lösen können, eigentlich sollte es ja nur eine Kleinigkeit sein?

                Wäre wirklich sehr dankbar!

                LG

                AlCalzone 1 Reply Last reply Reply Quote 0
                • U
                  uwe72 last edited by uwe72

                  OK i think i have a working solution now:

                  function createAlias(originalDatenpunkt, aliasDatenpunkt) {
                      setObject(aliasDatenpunkt, {
                          type: 'state',
                              common: {
                              name: getObject(originalDatenpunkt).common.name, 
                              type: getObject(originalDatenpunkt).common.type,  
                              unit: getObject(originalDatenpunkt).common.unit,    
                              read: true,
                              write: true,
                              role: getObject(originalDatenpunkt).common.role,    
                              alias: {
                                  id: originalDatenpunkt
                              }
                          },
                          native: {}
                      });
                  }
                  var originalDatenpunkt = "deconz.1.Lights.2.on";
                  var aliasDatenpunkt = "alias.0.test.mytest12_new2_NG";
                  createAlias(originalDatenpunkt, aliasDatenpunkt);
                  
                  1 Reply Last reply Reply Quote 0
                  • AlCalzone
                    AlCalzone Developer @uwe72 last edited by

                    @uwe72 sagte in (SOLVED) Create Alias in TypeScript:

                    eigentlich sollte es ja nur eine Kleinigkeit sein?

                    Ja, stand auch in der von dir zitierten Fehlermeldung 🙂

                    Property 'native' is missing in type 'TCustomObject' but required [...]

                    U 1 Reply Last reply Reply Quote 0
                    • U
                      uwe72 @AlCalzone last edited by

                      @alcalzone

                      Wie ist deine Meinung dann zu
                      https://forum.iobroker.net/topic/57264/typescript-kompilierfehler-setobject/5

                      Da gibts auch eine Fehlermeldung, für die "ich" eigentlich nicht verantwortlich sein kann?

                      😊

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post

                      Support us

                      ioBroker
                      Community Adapters
                      Donate

                      671
                      Online

                      31.8k
                      Users

                      80.0k
                      Topics

                      1.3m
                      Posts

                      3
                      9
                      576
                      Loading More Posts
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes
                      Reply
                      • Reply as topic
                      Log in to reply
                      Community
                      Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                      The ioBroker Community 2014-2023
                      logo