- This topic has 1 reply, 2 voices, and was last updated 7 years, 7 months ago by support-piotr.
-
AuthorPosts
-
gorandParticipantHellow!
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.
support-piotrParticipantGorand,
I think it’s because
l_param
has explicit typing set toITest2|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.
support-piotrParticipantGorand,
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
support-piotrParticipantGorand,
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/15289Best regards,
Piotr Tomiak -
AuthorPosts