Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeScript:函数重载不';不能使用泛型_Javascript_Typescript - Fatal编程技术网

Javascript TypeScript:函数重载不';不能使用泛型

Javascript TypeScript:函数重载不';不能使用泛型,javascript,typescript,Javascript,Typescript,我正在尝试让函数重载与采用泛型类型param的函数一起工作 最初我有一个函数foo interface Props<T, S> { fn?: (response: S) => T[]; enable?: boolean; } type Result = { irrelevant?: any; }; function foo<T, S>(props: Props<T, S>): Result { } 现在的问题是,使用此函数重载,f

我正在尝试让函数重载与采用泛型类型param的函数一起工作

最初我有一个函数
foo

interface Props<T, S> {
  fn?: (response: S) => T[];
  enable?: boolean;
}

type Result = {
  irrelevant?: any;
};

function foo<T, S>(props: Props<T, S>): Result {
  
}
现在的问题是,使用此函数重载,
foo
将要求除
T
S
之外的第三个通用参数
U
。所以我会有这个错误

应为3个类型参数,但得到2.ts(2558)


const{updater}=foo({/我没有找到修复此设计的方法,主要是因为这第三个类型参数。添加类型参数以实现此类目标通常是件麻烦事,因为typescript要么根本不接受类型参数,要么接受所有类型参数。要使typescript正确推断所有类型参数非常困难,因此在某些情况下,您可能希望您自己指定类型参数,但是您必须手动编写第三个参数,这非常烦人

因此,另一种解决方案是创建两种道具类型,如下所示:

export function foo<T, S>(props: EnabledProps<T, S>): CombinedResult;
export function foo<T, S>(props: DisabledProps<T, S>): Result;
export function foo<T, S>(props: Props<T, S>): CombinedResult | Result {
  // ...
}
//创建此类型以防止重复代码
接口公共道具{
fn?:(回答:S)=>T[];
}
接口禁用DProps扩展了CommonProps{
启用?:false
}
接口启用DProps扩展了CommonProps{
已启用:true
}
导出类型道具=禁用道具|启用道具
现在分别为禁用和启用的类型创建重载:

导出函数foo(props:enabledrops):CombinedResult;
导出函数foo(道具:DisabledProps):结果;
导出函数foo(props:props):CombinedResult |结果{
// ...
}
这应该行得通

const { updater } = foo<string, string[]>({ // I didn't found a way to fix this design, mainly because of this third type argument. Adding type arguments to achieve such goals is usually pain in the neck, because typescript accepts either no type arguments at all, or all of them. Making typescript infer everything correctly is very hard, so there will be cases where you will want to specify type parameters yourself, but then you will have to write the third one manually which is very annoying.

So another solution would be to create 2 prop types like this:

// Creating this type to prevent repeating code
interface CommonProps<T, S> {
  fn?: (response: S) => T[];
}
interface DisabledProps<T, S> extends CommonProps<T, S> {
  enabled?: false
}
interface EnabledProps<T, S> extends CommonProps<T, S> {
  enabled: true
}
export type Props<T, S> = DisabledProps<T, S> | EnabledProps<T, S>
export function foo<T, S>(props: EnabledProps<T, S>): CombinedResult;
export function foo<T, S>(props: DisabledProps<T, S>): Result;
export function foo<T, S>(props: Props<T, S>): CombinedResult | Result {
  // ...
}