Is there a way to hint the passed argument type to IDE (in my case Webstorm) in Javascript?

Koray Tugay

I have the following HTML file with some JavaScript in it:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
</head>
<body>
<div id="root"></div>
<div>
    <label for="new-content">
        <textarea id="new-content" style="width: 400px; height: 100px"></textarea>
    </label>
    <br/>
    <button onclick="addContent(document.getElementById('new-content'))">Submit</button>
</div>


<script>
    function addContent(/*HTMLTextAreaElement*/content) {
        alert(content.value);
    }
</script>
</body>
</html>

I like how I can hint Webstorm (or IntelliJ or Eclipse) what is the type of content is in function addContent, but I do not like how I can not tell what it is in onclick, which leads to following warning:

enter image description here

Here is my first world problem: Can I hint the type of document.getElementById('new-content') in the argument?

Koray Tugay

I figured out a way by trial and error, the following seems to work:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
</head>
<body>
<div id="root"></div>
<div>
    <label for="new-content">
        <textarea id="new-content" style="width: 400px; height: 100px"></textarea>
    </label>
    <br/>
    <button onclick="addContent(getNewContent())">Submit</button>
</div>

<div id="content"></div>

<script>
    function getNewContent() {
        return document.getElementById('new-content');/*HTMLTextAreaElement*/
    }

    function addContent(/*HTMLTextAreaElement*/content) {
        document.getElementById('content').innerHTML =
            document.getElementById('content').innerHTML +
            '<p>'.concat(content.value).concat('</p>');
    }
</script>
</body>
</html>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Type hint for a special argument

Type hint where one argument is the type of another

Type hint for argument of type A in method of class A

NetBeans - How to hint the IDE about file type?

JavaScript Document is not defined, WebStorm IDE

How to type hint specific objects in function argument

Most Pythonic way to convert None to an empty list in case a default argument is not passed in a Function?

WebStorm: Argument type this is not assignable to parameter type ObjectConstructor

The right way to type hint a Coroutine function?

Is there a way to specify a conditional type hint in Python?

Proper way to type hint a private property in python

Is there a way to make a type hint for multiple returns

Resolution of javascript resources/libraries/dependencies by the Webstorm IDE

Incorrect argument being passed to javascript

Is there proper way to alias type depending on passed template value argument at compile time (c++)

Why does TS give the following error in my case: Argument of type 'number' is not assignable to parameter of type 'never'?

How to write the type hint for an argument that can be of either class A or class B?

How to type hint function with a callable argument and default value

Is there a way to type hint that a tuple can be searched in a binary way?

What is the hidden argument being passed to my `MethodType`?

Why does the Streams API need a hint for generic type in this case?

Can someone please explain the type of params array in my case in JavaScript?

Are type arguments passed along with an argument to a method?

Error: "argument passed ... is not of type string."

Check if a file (passed as an argument to a script) is of .iso type

What type hint can I use in Python if my type is a function?

Is there a way to include spaces in my argument?

Can anyone hint at a way to recall my iterated labels in kivy?

Why does javascript constructor prints the argument passed to it?