facebook

Why the assignment does not work: var s:I1|string; s = s as string;

  1. CodeMix & Angular IDE
  2.  > 
  3. Webclipse 1.x Help
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #520147 Reply

    gorand
    Participant

    Hellow!
    Why the following code does not work:

    
    export interface ITest2 { IsTest?:boolean; }
    export function test2() {
    	var l_param:ITest2|string;
    	l_param = l_param as string;
    	l_param.indexOf("_"); <-- this line ERROR
    }
    

    see attached images

    Attachments:
    You must be logged in to view attached files.
    #520150 Reply

    support-piotr
    Participant

    Gorand,

    I think it’s because l_param has explicit typing set to ITest2|string and type inference is disabled on the variable. If you remove explicit typing on the variable, the problem is gone:

    
    export interface ITest2 { IsTest?:boolean; }
    export function test2() {
    	var l_param;
    	l_param = l_param as string;
    	l_param.indexOf("_"); //<-- no ERROR
    }
    

    Alternatively you can use a temporary variable to hold value with type inference enabled:

    
    export interface ITest2 { IsTest?:boolean; }
    export function test2() {
        var l_param: ITest2|string; ;
        let temp = l_param as string;
        temp.indexOf("_"); //<-- no ERROR
    }
    
    • This reply was modified 7 years, 7 months ago by support-piotr.
    #520165 Reply

    support-piotr
    Participant

    Gorand,

    This looks like a problem within the TypeScript compiler. I am not sure if your requirement is valid or not. Which version of TypeScript compiler is your project configured with? You can check which version is our tooling using for validation by: right click on the project > Properties > TypeScript.

    Best regards,
    Piotr Tomiak

    #520293 Reply

    support-piotr
    Participant

    Gorand,

    Thanks for your information. I’ve checked with TS 2.0 and TS 2.3 as well, and both expose the same issue. Unfortunately this is something out of our reach as the bug is within the TypeScript compiler itself and we are not making any modifications to it. Same issues happen if you run tsc directly from the command line. I have reported the problem to TypeScript developers: https://github.com/Microsoft/TypeScript/issues/15289

    Best regards,
    Piotr Tomiak

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: Why the assignment does not work: var s:I1|string; s = s as string;

You must be logged in to post in the forum log in