NEWS
(SOLVED) Create Alias in TypeScript
-
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 !!!!
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
-
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
-
@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
-
@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
-
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
-
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
-
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);
-
@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 [...]
-
Wie ist deine Meinung dann zu
https://forum.iobroker.net/topic/57264/typescript-kompilierfehler-setobject/5Da gibts auch eine Fehlermeldung, für die "ich" eigentlich nicht verantwortlich sein kann?