- This topic has 1 reply, 3 voices, and was last updated 7 years, 9 months ago by gorand.
-
AuthorPosts
-
gorandParticipantHellow!
I declare two attributes in the function, but the system always tries to use a simple type for this attribute. How to get the system to not report an error when using the second type attribute?
See attached image.function test(txt:string|{Text:string}, p?:{Text:string}|Function, fn?:Function) { if(typeof txt == "object") { p = txt as any; txt = p.Text; /*<- this error*/ } }
Attachments:
You must be logged in to view attached files.
support-swapnaModeratorGorand,
Thank you for the screenshot.
Here is the code which demonstrates how to declare multiple types for the attribute :
class Text { constructor(public Text: string){} }; function test(txt: Text | string, p? : Text | Function, fn?:Function) { if (typeof txt == "Text") { p = txt; txt = p.Text; } } test(new Text("bar")); test("foo")
Hope this helps. Please let us know if you have any questions.
–Swapna
MyEclipse Support
gorandParticipantIt would be nice to automate this behavior. Otherwise you have to indicate not very comfortable for the types of treatment to the required field. For example:
function testFunc(pp:Function|{fn?:Function, name1:number, name2:string, name3:{name1:Array<{name1:string, name2:number}>}, name4:any}) { var fn:Function; if(isFunction(pp)) { fn = pp as Function; pp = {} as any; } else { fn = (pp as any).fn; } // how remove cast to any // next line for TypeScript auto typing but this line not need compiled JavaScript (pp=pp;) and this line is very big from TypeScript // and next line contains too much typing, which is not very convenient if you change typing in the parameters of this method - you will need to make a new type, and here. /*begin remove from compiled*/ // How to automate this type cast? pp = pp as {name1:number, name2:string, name3:{name1:Array<{name1:string, name2:number}>}, name4:any}; /*end remove from compiled*/ fn.call(pp.name4); if(pp.name1 == 1) {} if(pp.name3.name1) {} }
How to automate type cast pp = pp as typing and remove many cast as any?
Brian FernandesModeratorgorand,
I’m afraid we couldn’t understand what you meant. Do you mind explaining in more detail? Are you looking for a code suggestion, or refactoring support from the IDE?
gorandParticipantIt seems to me that it would be more convenient, if your system in the event of an indication of several types in the code field perceived all these types.
To the code did not cause errors:function test(pp:number|string|any[]) { var i=pp+1; // auto use number from multiply types var s=pp.substring(1); // auto use string from multiply types var arr=pp.concat([123]); // auto use Array from multiply types }
If your system will not do so – then I need you to tell me how to constantly prompted to write all the fields when I need to specify your system, what specific type at the moment I’m going to use the code:
function test(pp:string|{n1:string,n2:string,n3:string,n4:string,n5:string,n6:string,n7:string,...,nn:string}) { var o=pp as {n1:string,n2:string,n3:string,n4:string,n5:string,n6:string,n7:string,...,nn:string}; <-- how simplify this line }
Attachments:
You must be logged in to view attached files.
support-swapnaModeratorgorand,
Thank you for the explanation and the screenshots. I have escalated it to the dev team.
They will get back to you soon.–Swapna
MyEclipse Support
gorandParticipantHellow!
I myself found a way to shorten the declaration of a complex type in a function. This is done through function generic types.
for example:function test<TT extends {n1:string,n2:any,n3:Array<string>,n4:{},n5:number,n6:ITester,...,nn:any}> (pp:string|TT) { var n:string; if(isStr(pp)) { pp = {n1:pp} as TT; } pp = pp as TT; // <-- this line TT is shortly word n = pp.n1; }
this example TT is declared once and is short word
- This reply was modified 7 years, 9 months ago by gorand.
-
AuthorPosts