有没有办法在Javascript中将传递的参数类型提示给IDE(在我的案例中是Webstorm)?

问题描述 投票:0回答:1

我有以下HTML文件,其中包含一些JavaScript

<!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>

我喜欢我可以提示Webstorm(或IntelliJ或Eclipse)contentfunction addContent的类型是什么,但我不喜欢我怎么也不知道它在onclick中是什么,这会导致以下警告:

enter image description here

这是我的第一个世界问题:我可以在论证中暗示document.getElementById('new-content')的类型吗?

javascript intellij-idea webstorm type-hinting
1个回答
0
投票

我通过反复试验找到了一种方法,以下似乎有效:

<!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>
© www.soinside.com 2019 - 2024. All rights reserved.