如何为采用参数并在内部使用 "arguments" 的函数添加流类型
How to add flowtype for functions that take parameters and also use "arguments" internally
以下代码来自 OSS 库(适用于 VDOM)。我正在尝试将流类型添加到生成虚拟 DOM 节点的 h
函数。但是请注意,该函数内部使用 arguments
?它还需要两个输入。想知道如何输入注释。
- 第一个参数是字符串或函数。
- 第二个参数是一个包含大量键值的对象
它在内部使用参数,因为函数可以接受 N 个参数。
一个。所以函数必须至少有 2 个参数(但可以有 N 个参数)。
b。从第 3 个参数开始,这些参数必须是 Array
或 string
类型。
所以你可以这样称呼:
h("b", {...})
或
h(()=>{}, {...}, [])
或
h(()=>{}, {...}, [], [], []..)
h(()=>{}, {...}, [], "string", "string2")
var i
var stack = []
export function h(tag, props) {
var node
var children = []
for (i = arguments.length; i-- > 2; ) {
stack.push(arguments[i])
}
while (stack.length) {
if (Array.isArray((node = stack.pop()))) {
for (i = node.length; i--; ) {
stack.push(node[i])
}
} else if (node != null && node !== true && node !== false) {
children.push(typeof node === "number" ? (node = node + "") : node)
}
}
return typeof tag === "string" ? {
tag: tag,
props: props || {},
children: children
}
: tag(props, children)
}
您需要做的是为您正在使用的库创建一个 libdef
并将其放入项目根目录的 flow-typed
文件夹中。
或者您也可以提交到 flow-typed
repository.
制作 libdef
并不难。您只需创建一个包含以下内容的文件 library-name.js
并在其中声明所有函数类型:
declare module 'my-third-party-library' {
declare type arg1Type = string | () => any;
declare type arg2Type = {
[key: string]: any,
};
declare type restArgsType = Array<Array<any> | string>;
declare class className {
h(arg1: arg1Type, arg2: arg2Type, ...restArgs: restArgsType): void;
}
declare var exports: className;
}
原答案:
I think you can just specify the types of the arguments of the
function like this:
type arg1Type = string | () => any;
type arg2Type = {
[key: string]: any,
}
type restArgsType = Array<Array<any> | string>
function h(arg1: arg1Type, arg2: arg2Type, ...restArgs: restArgsType){}
Afterwards you can just use the function like:
h('b', obj);
h(func, obj, []);
h(func, obj, [], []);
h(func, obj, [], [], []);
h(func, obj, [], 'string', 'string2')
As you can see on flow.org/try
以下代码来自 OSS 库(适用于 VDOM)。我正在尝试将流类型添加到生成虚拟 DOM 节点的 h
函数。但是请注意,该函数内部使用 arguments
?它还需要两个输入。想知道如何输入注释。
- 第一个参数是字符串或函数。
- 第二个参数是一个包含大量键值的对象
它在内部使用参数,因为函数可以接受 N 个参数。
一个。所以函数必须至少有 2 个参数(但可以有 N 个参数)。
b。从第 3 个参数开始,这些参数必须是
Array
或string
类型。
所以你可以这样称呼:
h("b", {...})
或h(()=>{}, {...}, [])
或h(()=>{}, {...}, [], [], []..)
h(()=>{}, {...}, [], "string", "string2")
var i var stack = [] export function h(tag, props) { var node var children = [] for (i = arguments.length; i-- > 2; ) { stack.push(arguments[i]) } while (stack.length) { if (Array.isArray((node = stack.pop()))) { for (i = node.length; i--; ) { stack.push(node[i]) } } else if (node != null && node !== true && node !== false) { children.push(typeof node === "number" ? (node = node + "") : node) } } return typeof tag === "string" ? { tag: tag, props: props || {}, children: children } : tag(props, children) }
您需要做的是为您正在使用的库创建一个 libdef
并将其放入项目根目录的 flow-typed
文件夹中。
或者您也可以提交到 flow-typed
repository.
制作 libdef
并不难。您只需创建一个包含以下内容的文件 library-name.js
并在其中声明所有函数类型:
declare module 'my-third-party-library' {
declare type arg1Type = string | () => any;
declare type arg2Type = {
[key: string]: any,
};
declare type restArgsType = Array<Array<any> | string>;
declare class className {
h(arg1: arg1Type, arg2: arg2Type, ...restArgs: restArgsType): void;
}
declare var exports: className;
}
原答案:
I think you can just specify the types of the arguments of the function like this:
type arg1Type = string | () => any; type arg2Type = { [key: string]: any, } type restArgsType = Array<Array<any> | string> function h(arg1: arg1Type, arg2: arg2Type, ...restArgs: restArgsType){}
Afterwards you can just use the function like:
h('b', obj); h(func, obj, []); h(func, obj, [], []); h(func, obj, [], [], []); h(func, obj, [], 'string', 'string2')
As you can see on flow.org/try