Why does ‘instanceof’ in TypeScript give me the error “‘Foo’ only refers to a type, but is being used as a value here.”?

TL;DR instanceof works with classes, not interfaces nor type aliases. What’s TypeScript trying to tell me? The issue is that instanceof is a construct from JavaScript, and in JavaScript, instanceof expects a value for the right-side operand. Specifically, in x instanceof Foo JavaScript will perform a runtime check to see whether Foo.prototype exists anywhere in … Read more

typeof for RegExp

You can use instanceof operator: var t = /^foo(bar)?$/i; alert(t instanceof RegExp);//returns true In fact, that is almost the same as: var t = /^foo(bar)?$/i; alert(t.constructor == RegExp);//returns true Keep in mind that as RegExp is not a primitive data type, it is not possible to use typeof operator which could be the best option … Read more

C++ equivalent of java’s instanceof

Try using: if(NewType* v = dynamic_cast<NewType*>(old)) { // old was safely casted to NewType v->doSomething(); } This requires your compiler to have rtti support enabled. EDIT: I’ve had some good comments on this answer! Every time you need to use a dynamic_cast (or instanceof) you’d better ask yourself whether it’s a necessary thing. It’s generally … Read more

Why does instanceof return false for some literals?

Primitives are a different kind of type than objects created from within Javascript. From the Mozilla API docs: var color1 = new String(“green”); color1 instanceof String; // returns true var color2 = “coral”; color2 instanceof String; // returns false (color2 is not a String object) I can’t find any way to construct primitive types with … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)