是否有一种方法可以让javascript函数知道某个参数是某个类型的?

能够做这样的事情将是完美的:

function myFunction(Date myDate, String myString)
{
    //do stuff
}

谢谢你!

更新:作为答案是一个响亮的“不”,如果我想myDate被视为一个日期(为了在它上调用日期函数),我必须将其转换为函数内的日期或设置一个日期类型的新变量?


当前回答

看看来自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

下面是如何运行它。

其他回答

TypeScript是目前最好的解决方案之一。

TypeScript通过向语言中添加类型来扩展JavaScript。

Typescript版本演示

// type alias
type myDateType = Date;
type myStringType = string;

function myFunction(myDate: myDateType, myString: myStringType) {
  // do stuff
  console.log(`myDate =`, myDate);
  console.log(`myString =`, myString);
}

myFunction(new Date(), 'TypeScript is awesome!');

试试这个在线游乐场

refs

https://www.typescriptlang.org/

虽然不能将类型通知JavaScript语言,但可以通知IDE,因此可以获得更有用的自动补全。

这里有两种方法:

Use JSDoc, a system for documenting JavaScript code in comments. In particular, you'll need the @param directive: /** * @param {Date} myDate - The date * @param {string} myString - The string */ function myFunction(myDate, myString) { // ... } You can also use JSDoc to define custom types and specify those in @param directives, but note that JSDoc won't do any type checking; it's only a documentation tool. To check types defined in JSDoc, look into TypeScript, which can parse JSDoc tags. Use type hinting by specifying the type right before the parameter in a /* comment */: This is a pretty widespread technique, used by ReactJS for instance. Very handy for parameters of callbacks passed to 3rd party libraries.

打印稿

对于实际的类型检查,最接近的解决方案是使用TypeScript,它是JavaScript的超集。这里是5分钟内的TypeScript。

看看来自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

下面是如何运行它。

也许是这样的辅助函数。但如果你发现自己经常使用这种语法,你可能应该切换到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
}

您可以在函数中使用包装器来实现自动处理类型检查的系统。

使用这种方法,您可以构建一个完整的声明式类型检查系统,该系统将为您管理类型检查。 如果您有兴趣更深入地了解这个概念,请查看Functyped库

下面的实现以一种简单但有效的方式说明了主要思想:

/* * checkType() : Test the type of the value. If succeds return true, * if fails, throw an Error */ function checkType(value,type, i){ // perform the appropiate test to the passed // value according to the provided type switch(type){ case Boolean : if(typeof value === 'boolean') return true; break; case String : if(typeof value === 'string') return true; break; case Number : if(typeof value === 'number') return true; break; default : throw new Error(`TypeError : Unknown type provided in argument ${i+1}`); } // test didn't succeed , throw error throw new Error(`TypeError : Expecting a ${type.name} in argument ${i+1}`); } /* * typedFunction() : Constructor that returns a wrapper * to handle each function call, performing automatic * arguments type checking */ function typedFunction( parameterTypes, func ){ // types definitions and function parameters // count must match if(parameterTypes.length !== func.length) throw new Error(`Function has ${func.length} arguments, but type definition has ${parameterTypes.length}`); // return the wrapper... return function(...args){ // provided arguments count must match types // definitions count if(parameterTypes.length !== args.length) throw new Error(`Function expects ${func.length} arguments, instead ${args.length} found.`); // iterate each argument value, and perform a // type check against it, using the type definitions // provided in the construction stage for(let i=0; i<args.length;i++) checkType( args[i], parameterTypes[i] , i) // if no error has been thrown, type check succeed // execute function! return func(...args); } } // Play time! // Declare a function that expects 2 Numbers let myFunc = typedFunction( [ Number, Number ], (a,b)=>{ return a+b; }); // call the function, with an invalid second argument myFunc(123, '456') // ERROR! Uncaught Error: TypeError : Expecting a Number in argument 2