Is it possible to typedef things in javascript somehow? Maybe by getting it from the prototype
object or something?
For instance, i'd like to typedef the var
keyword.
var string = prototype.var;
So now in stead of using 'var' i can use:
string blaat = "It's like using the 'var' keyword";
Is this possible somehow in javascript?
JsDoc provides two types of tags: block tags and inline tags. Block tags provide means to annotate variables, functions, arguments, modules, namespaces, return types, and classes and describe what a specific code snippet does. These could be types, arguments, callbacks, modules, namespaces, etc.
You can use most JSDoc type syntax and any TypeScript syntax, from the most basic like string to the most advanced, like conditional types.
JSDoc comments are used for documentation lookup with Ctrl+Q in JavaScript and TypeScript, see JavaScript documentation look-up and TypeScript documentation look-up, as well as for type annotations and method return type hints in chained methods.
typedef is a reserved keyword in the programming languages C and C++. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.
No, this is not possible. Period.
Your example is also an impossible thing to want, even though I understand the motivation. JavaScript is dynamically typed. You cannot declare variables to be string. And in that light the whole statement string x = "foo";
is pointless.
EDIT Yes, it's possible to achieve this effect with TypeScript. No, TypeScript is not JavaScript. The question was about the latter. That you can do a similar thing in a completely different programming language does not make this answer incorrect or obsolete.
Declaring a variable as, e.g., string
will remain impossible in JavaScript until the day when the ECMAScript Standard adds static typing to the language.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With