是否有一种方法可以让javascript函数知道某个参数是某个类型的?
能够做这样的事情将是完美的:
function myFunction(Date myDate, String myString)
{
//do stuff
}
谢谢你!
更新:作为答案是一个响亮的“不”,如果我想myDate被视为一个日期(为了在它上调用日期函数),我必须将其转换为函数内的日期或设置一个日期类型的新变量?
是否有一种方法可以让javascript函数知道某个参数是某个类型的?
能够做这样的事情将是完美的:
function myFunction(Date myDate, String myString)
{
//do stuff
}
谢谢你!
更新:作为答案是一个响亮的“不”,如果我想myDate被视为一个日期(为了在它上调用日期函数),我必须将其转换为函数内的日期或设置一个日期类型的新变量?
当前回答
解释
我不确定我的答案是否直接回答了最初的问题,但我想很多人来这里只是为了找到一种方法让他们的ide理解类型,我将分享我的发现。
如果你想让VSCode理解你的类型,请按照下面的步骤进行。请注意,js运行时和NodeJS根本不关心这些类型。
解决方案
1-用.d创建一个文件。Ts结尾:例如:index.d.ts。您可以在另一个文件夹中创建此文件。例如:types/index.d.ts 假设我们想要一个叫做view的函数。将这些行添加到index.d.ts中:
/**
* Use express res.render function to render view file inside layout file.
*
* @param {string} view The path of the view file, relative to view root dir.
* @param {object} options The options to send to view file for ejs to use when rendering.
* @returns {Express.Response.render} .
*/
view(view: string, options?: object): Express.Response.render;
3-创建jsconfig。Json文件在你的项目的根。(看起来仅仅创建这个文件就足以让VSCode搜索你的类型了)。
再多一点
现在假设我们想把这个类型添加到另一个库类型中。(就我自己的情况而言)。我们可以使用一些ts的关键字。只要VSCode理解ts,我们就没有问题。 例如,如果你想把这个视图函数添加到response from expressjs中,修改index.d.ts文件如下:
export declare global {
namespace Express {
interface Response {
/**
* Use express res.render function to render view file inside layout file.
*
* @param {string} view The path of the view file, relative to view root dir.
* @param {object} options The options to send to view file for ejs to use when rendering.
* @returns {Express.Response.render} .
*/
view(view: string, options?: object): Express.Response.render;
}
}
}
结果
其他回答
我假设您允许IDE帮助您;那么下面的答案可能会帮助你。
IDE: jetbrains/Golang如果你的IDE不是这个也没关系。我相信所有支持JSDoc的IDE都能满足您的大部分需求。
它可以很好地显示JSDoc。
Demo
我的- pkg base64编码- js。
/**
* Convert string to the base64 format.
*
* @param str {string} Input string
* @returns {string} some message about return...
* @example
* - btoa(toBinary("☸☹☺☻☼☾☿"))
* - Str2base64("☸☹☺☻☼☾☿")
* @see https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/btoa#unicode_strings
*/
export function Str2base64(str) {
return btoa(toBinary(str))
}
. js
import * as base64 from "../pkg/encoding/base64"
const filenameB64 = base64.Str2base64("test")
有用的JSDoc文档
JSDoc块标签 显示@returns, @param, @type…
其他链接
jetbrains JSDoc /评论
也许是这样的辅助函数。但如果你发现自己经常使用这种语法,你可能应该切换到TypeScript。
function check(caller_args, ...types) {
if(!types.every((type, index) => {
if(typeof type === 'string')
return typeof caller_args[index] === type
return caller_args[index] instanceof type;
})) throw Error("Illegal argument given");
}
function abc(name, id, bla) {
check(arguments, "string", "number", MyClass)
// code
}
使用typeof或instanceof:
const assert = require('assert');
function myFunction(Date myDate, String myString)
{
assert( typeof(myString) === 'string', 'Error message about incorrect arg type');
assert( myDate instanceof Date, 'Error message about incorrect arg type');
}
看看来自Facebook的新Flow库,“一个静态类型检查器,旨在发现JavaScript程序中的类型错误”
定义:
/* @flow */
function foo(x: string, y: number): string {
return x.length * y;
}
foo('Hello', 42);
类型检查:
$> flow
hello.js:3:10,21: number
This type is incompatible with
hello.js:2:37,42: string
下面是如何运行它。
不,JavaScript不是静态类型语言。有时可能需要手动检查函数体中的参数类型。