TypeScript cast (assertion) results in Syntax Error compiler warning? - visual-studio

I've got a TypeScript script that has to interact with a third-party vendor that uses global functions as callbacks (you can't pass in a callback). For instance, to "listen" for a result from their "API", you define the function SetElqContent. E.g.,
window.SetElqContent = function(){/* handle result */};
When the TypeScript compiler sees this line, it complains that The property 'SetElqContent' does not exist on value of type 'Window'.
I thought I could get around this by simply casting to type "any". Actually, this isn't type casting but type assertion, but I think of it as casting, although I understand it's not quite the same. So, I tried:
(<any>window).SetElqContent = function(){/* handle result */};;
To m y surprise, this results in Syntax error, and the line number and column points to the < character in the <any> cast. I tried a few other variants, and I get Syntax error on the initial < of the cast no matter what kind of cast I was doing:
var windowAny = <any>window;
var docElement = <HTMLElement>window.document;
What is it about my type assertions that is invalid syntax?
I'm using Visual Studio 2013 with Update 2, which has a "compile on save" feature for TypeScript files. That's how I'm compiling my .ts files, and it's from in Visual Studio where the Syntax error message is emitted.
UPDATE: Apparently this is related to Visual Studio. When I use the standalone tsc compiler to compile the same file, it emits no errors or warnings.

Apparently my syntax is correct but there is a bug in the Visual Studio tooling. I can't provide exact reproduce steps, and in fact, deleting everything in the .ts file, saving, then restoring the code (via ctrl-z) and resaving caused the "syntax error" warning to disappear.
If I can determine any more specifics about what causes this issue to manifest, I'll report back.

Best way is to create a type definitions file for it
If the library name is eloqua.js, you create a eloqua.d.ts file and refer to it in your .js file like
/// < reference path="../typings/eloqua.d.ts" />
There are many type definition files online available at definitelyTyped website.
https://github.com/borisyankov/DefinitelyTyped
You can contribute yours to there as well.

If you extend the Window interface definition, you'll remove the error:
interface Window {
SetElqContent: Function;
}
window.SetElqContent = function(){/* handle result */};

Here is how you can do the assertion properly:
function SetElqContent(){/* handle result */};
// FINE
(<any>window).SetElqContent = SetElqContent;
or
// FINE
(<any>window).SetElqContent = function SetElqContent(){/* handle result */};
However you should avoid asserting and just do what Steve Fenton recommends as it is more discoverable
Update
Demo in VS:

Related

Access source code information from the Visual Studio editor

I'm trying to get into creating Visual Studio extensions. There are a few examples in the online documentation from Microsoft about creating custom stuff but I can't find anything on how to access Intellisense (or any other code analysis).
I know there are some questions about this topic (namely how do you get c++ Intellisense data in a visual Studio Extention?) but those are from 2012 or earlier and not up to date anymore (at least I hope so).
The following is only applied to C++ source code.
What I specifically want, is to examine the expression that the cursor is at. Then I want to check whether the expression is either
an object declaration / instanciation, like string s("my string");, or
a function call, like std::max(1, 2);
From those expressions I want to get the full qualified name (including the namespace) of the type / function and possibly the types of the function / constructor arguments. For example:
// ...
using namespace std;
auto x = max(1, 2);
// ...
Now, if the cursor enters max I need the full qualified name ::std::max and the argument types [int, int].
Another example:
// ...
using namespace std;
string s("my string");
// ...
Here I need the full qualified name ::std::string and the argument types [const char*, std::allocator].
Is this somehow possible? I'm also interested in partial solutions. Any guidance or hints to some sort of documentation is very welcome.
We can only get partial information via code model.
https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.vccodemodel.vccodemodel?view=visualstudiosdk-2017

Visual Studio Intellisense error in range-based for loop

My Visual Studio 2015 IDE (Community Edition) keeps complaining about the code below
struct item
{
int data;
std::vector<item*> linkedItems;
};
void traverseLinkedItems(item* p_item)
{
for (item* i : p_item->linkedItems) // Intellisense Error: A value of type "item*" cannot be used to initialize an entity of type "item*"
{
std::cout << i->data;
}
}
It compiles just fine. So I'm not sure whether it's a bug in VS, or am I missing something?
When I use auto instead of specifying the type of i explicitly, everything is OK.
Thanks!
IntelliSense is not always right. In order to know everything about your code, it would have to fully compile it, but this would be too slow to be (conveniently) usable. Instead, it parses your code in a faster but less complete way to get autocompletion information and find obvious errors. However, sometimes it trips over something, either due to a bug or because it did not manage to obtain all the information it needs. When this happens, its output is not so (Intelli)sensible.
I would simply ignore the IntelliSense error, or, like you said, use auto instead of item* if you want to get rid of the error. Maybe in a later update, or after changes to your code, the error will magically disappear.

How to view the result of an expression in MSVS2013?

I remember seeing somewhere that you can specify which dll to get the address of symbols so that one can use that variable in the watch window. I can't for the life of me remember where I saw this. The best that I can come up with is Format Specifiers in C++.
The reason I want this is so that I can see the visibility status of a window and MSVS keeps saying that identifier "IsWindowVisible" is undefined.
I was trying to use something like the following in the watch window:
::IsWindowVisible(m_hWnd),user32.dll
Using:
this->IsWindowVisible()
results in Function CWnd::IsWindowVisible has no address, possibly due to compiler optimizations. which is why I'm trying to use the win32 call. Ideas?
http://msdn.microsoft.com/en-nz/library/y2t7ahxk.aspx
Haven't tried it, but it seems to me that IsWindowVisible(m_hWnd) should work, or maybe IsWindowVisible(this->m_hWnd).

C++ Builder F1007 Irreducible Expression Tree

I'm using C++ builder XE under Windows 7 pro.
I'm currently stepping through a function and want to inspect the value of some of my variables. There's a character array I've got, local to a function.
char result[80];
When I try to inspect this (with the code paused inside this function), a message pops up :
Error inspecting 'result': F1007 Irreducible expression tree
If I try to add a watch to this variable, it says "???".
Any ideas what could cause this, and what it could mean in this context ?
According to the documentation:
F1007 Irreducible expression tree (C++)
An expression on the indicated line of the source file caused the code generator to be unable to generate code. Avoid using the expression. Notify Embarcadero if an expression consistently reproduces this error.
You are likely encountering a codegen bug and should file a Quality Central ticket to Embarcadero for review.

Visual studio 2010: how to watch a memory hex location

I tried some suggestions found online but it does not work for me. Im using Visual Studio 2010. Basically I typed loc(kcs(1,4)) (thats my variable) and I obtained 157510036. Its hex is 9636994. So then I typed (INTEGER*)0x9636994 but on the watch window under the "value"column it says "undefined variable INTEGER". I trid lowercase integer or real and same answer. Any suggestion?
I typed (INTEGER*)0x9636994 but on the watch window under the "value"column it says "undefined variable INTEGER".
According to Restrictions on Native C++ Expressions:
Type Casting
If you cast to a type, the type must be known to the debugger. You must have another object of that type in your program. Types created using typedef statements are not supported.
Try using the underlying type. So, for example, if INTEGER is actually an int you would try to watch (int *)0x9636994.
This also assumes that the variable is fixed at 0x9636994 (basically that you're not trying to refer to something transient on the stack).

Resources