Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

Is there a built-in way to test if a variable is of a specific type?

blackslateblackslate Member Posts: 16
The documentation indicates that there are 9 standard types:
  • Boolean
  • Number
  • String
  • Array
  • Map
  • Box
  • Function
  • Builtin
  • Undefined
However, I have found no standard method to check if a given variable is of a given type. Have I not been looking hard enough?

I have found a workaround, by creating a `testType` function and overloading it for each type, so that the different overloaded versions return the name of the type that they receive as an argument.

function testType(value is array) {<br>&nbsp;&nbsp;&nbsp; return "array";<br>}<br><br>function testType(value is boolean) {<br>&nbsp;&nbsp;&nbsp; return "boolean";<br><div>}</div><div><br></div><div>// ... and so on<br><br>function isArray (value) {<br>    return (testType(value) == "array");<br>}<br></div>
(The Format > Code option makes the code above look terrible in Preview. Let's hope it appears correctly in the forum.)

You can find my proof of concept here.

Is my workaround really necessary, or is there a built-in method that I have overlooked?





Best Answers

Answers

Sign In or Register to comment.