0

In C# you could do something like this :

string typeName = typeof(int).FullName;

And the value of typeName would be System.Int32. (Refer this question for reference).

So, my question is, how can I achieve the same thing in JavaScript or TypeScript?

Please note that I do not want to get the type name from an object, I only want to get it from a type just like the example code above.

Wong Jia Hau
  • 2,639
  • 2
  • 18
  • 30
  • i think it makes very little sense to want to do this in javascript, since ; a : Javascript is weakly typed (try doing `"1" == true`), and b : the only types javascript knows are fairly descriptive of what they are. why would you need those 6 or so types with a "strong" name? – Timothy Groote Oct 26 '17 at 14:43
  • the only thing i can find that comes close to what you are asking is doing something like `typeof Number()`, which would output *"number"* (shocking, right?) – Timothy Groote Oct 26 '17 at 14:49
  • (but don't do `typeof Number`, because that would return *"function"*) – Timothy Groote Oct 26 '17 at 14:54
  • To be frank, I actually wanted to use this feature in TypeScript – Wong Jia Hau Oct 27 '17 at 09:24

3 Answers3

1

In Javascript you can use

typeof variable_name
Tanmay
  • 1,123
  • 1
  • 10
  • 20
  • 1
    It's not a function, it's a keyword, so the parentheses aren't needed. – Tom Fenech Oct 26 '17 at 14:42
  • @Tom yes you are right so I have corrected the answer – Tanmay Oct 26 '17 at 14:53
  • @Tanmay Thanks for your answer but this is not the answer I want, as I already mentioned in the question that I don't want to get the type name from an object. I want to get it directly from the type itself. – Wong Jia Hau Oct 26 '17 at 15:29
  • @Wong Jia Hau In Javascript there is only one way to declaring variable ie. var variable_name; So using Javascript, it is not achievable what you are trying to. – Tanmay Oct 27 '17 at 10:20
0

I just found out that the type-name package in Node.js can solve this problem.

Wong Jia Hau
  • 2,639
  • 2
  • 18
  • 30
-2

var typeName = 5;

so you can put a simple alert in javascript.

an example like,

alert(typeof typeName);

just it.

  • this wouldn't work in javascript *or* typescript, and it makes no sense. – Timothy Groote Oct 26 '17 at 14:35
  • i already try many times bro, if just you want to know your variable type, just using typeof its the simpliest way. https://codepen.io/iwanWijaya/pen/eeOmmj?editors=1111 – Iwan Wijaya Oct 26 '17 at 14:41
  • i'm talking about the code you posted here. try running `var string typeName = 5` in a console, or in typescript, and it will tell you there is a syntax error. – Timothy Groote Oct 26 '17 at 14:44
  • my bad, i just forgot, just put away the string word. blame on me. – Iwan Wijaya Oct 26 '17 at 14:49
  • @IwanWijaya While you are improving your answer. Did you ever hear of `code tags`? Or the senses of explaining things? Take a look at [How do I write a good answer](https://stackoverflow.com/help/how-to-answer) for reference. – Clijsters Oct 26 '17 at 14:54