Issue
Does JS expose a list of primitive types that can be accessed programmatically, or is there a built in method to find out if a string is a type? This is a somewhat contrived language-based question and not intended to solve a specific algorithm.
Besides manually hard-coding array of ['string', 'number', 'boolean', etc...], is there a way to programmatically get that list?
Solution
There is no definitive way to get a list of typeof results built into JavaScript itself.
If they are exposed somewhere, that would be through a library or as part of the API of a specific environment.
In TypeScript you can define a type that covers all possible primitive types as shown in this answer:
const uselessVariable = typeof (1 as any);
// type Test = "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
type Test = typeof uselessVariable;
In TypeScript the JavaScript typeof operator is assigned a return type which is restricted to only the results that are possible for it.
Answered By - VLAZ
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.