[英]Typescript use parameter type inference with defaults
我有如下功能,效果很好:
function pickAttributes<K extends string, T> (e: Element, attrs: K[]): Record<K, string> {
return attrs.reduce((obj, key) => {
return {
...(obj as any),
[key]: e.getAttribute(key)
}
}, {})
}
我希望该函数可以使用可选的map函数将属性转换为其他类型。
function pickAttributes<K extends string, T> (e: Element, attrs: K[], mapFn?: (attr: null | string) => T): Record<K, T> {
if (!mapFn) mapFn = x => String(x)
return attrs.reduce((obj, key) => {
return {
...(obj as any),
[key]: mapFn(e.getAttribute(key))
}
}, {})
}
但是我得到这个错误
TS2322:
Type '(x: string | null) => string' is not assignable to type '((attr: string | null) => T) | undefined'.
Type '(x: string | null) => string' is not assignable to type '(attr: string | null) => T'.
Type 'string' is not assignable to type 'T'.
如果我尝试使用默认参数,则会收到类似的错误。
更改后的功能似乎可以正常运行,而无需尝试添加默认值
function pickAttributes<K extends string, T> (e: Element, attrs: K[], mapFn?: (attr: null | string) => T): Record<K, T> {
return attrs.reduce((obj, key) => {
return {
...(obj as any),
[key]: e.getAttribute(key)
}
}, {})
}
首先:我不确定什么可以扩展字符串,所以我将删除通用参数K。
但是无论如何:您希望您的返回类型根据mapFn参数的存在而变化。 我将通过定义函数的两个重载来解决这一问题。 并且在实现中,通过使用联合类型string | T
string | T
:
function pickAttributes<K extends string>(e: Element, attrs: K[]): Record<K, string>;
function pickAttributes<K extends string, T> (e: Element, attrs: K[], mapFn: (attr: null | string) => T): Record<K, T>;
function pickAttributes<K extends string, T> (e: Element, attrs: K[], mapFn?: (attr: null | string) => T): Record<K, string | T> {
const fn = mapFn || (x => String(x));
return attrs.reduce((obj, key) => {
return {
...(obj as any),
[key]: fn(e.getAttribute(key))
}
}, {});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.