Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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
参数数目可变的函数的Typescript泛型_Typescript_Generics_Types - Fatal编程技术网

参数数目可变的函数的Typescript泛型

参数数目可变的函数的Typescript泛型,typescript,generics,types,Typescript,Generics,Types,我有一个函数,可以接受任意数量的类 function findObjects(...Classes) { ... } 所有类都从一个组件继承。此函数以某种方式搜索作为这些类实例的对象 如何为参数数量可变的函数实现模板 应该是这样的: function findObjects(...Classes): [...Classes] { ... } 这对于类型的正确定义和intellisense的正确操作是必要的。您可能想看看,它们用于模拟具有。一种可能的解决办法: // We pass in a

我有一个函数,可以接受任意数量的类

function findObjects(...Classes) { ... }
所有类都从一个
组件继承。此函数以某种方式搜索作为这些类实例的对象

如何为参数数量可变的函数实现模板

应该是这样的:

function findObjects(...Classes): [...Classes] { ... }

这对于类型的正确定义和intellisense的正确操作是必要的。

您可能想看看,它们用于模拟具有。一种可能的解决办法:

// We pass in a tuple of classes. This tuple is generically typed (type parameter T)
// { new(...arg: any): any } is constructor function type for a class
function findObjects<T extends { new(...arg: any): any }[]>(...cls: T): Partial<T> {
  // search for objects that are instances of these classes; return filtered classes
}

class Component { c = "c" }
class A extends Component { a = "a" }
class B extends Component { b = "b" }

const res = findObjects(A, B) // [(typeof A | undefined)?, (typeof B | undefined)?]
//我们传入一个类的元组。此元组是泛型的(类型参数T)
//{new(…arg:any):any}是类的构造函数类型
函数findObjects(…cls:T):部分{
//搜索作为这些类的实例的对象;返回筛选的类
}
类组件{c=“c”}
类A扩展组件{A=“A”}
类B扩展组件{B=“B”}
const res=findObjects(A,B)/[(A的类型未定义)?,(B的类型未定义)?]
对于返回类型,我使用了
Partial
,它是传入类中的一个。如果您的
findObjects
实现过滤掉了相应的类项,则它在索引处包含
undefined
(根据需要调整返回类型)


您可能想看一看,这是用来模拟函数的。一种可能的解决办法:

// We pass in a tuple of classes. This tuple is generically typed (type parameter T)
// { new(...arg: any): any } is constructor function type for a class
function findObjects<T extends { new(...arg: any): any }[]>(...cls: T): Partial<T> {
  // search for objects that are instances of these classes; return filtered classes
}

class Component { c = "c" }
class A extends Component { a = "a" }
class B extends Component { b = "b" }

const res = findObjects(A, B) // [(typeof A | undefined)?, (typeof B | undefined)?]
//我们传入一个类的元组。此元组是泛型的(类型参数T)
//{new(…arg:any):any}是类的构造函数类型
函数findObjects(…cls:T):部分{
//搜索作为这些类的实例的对象;返回筛选的类
}
类组件{c=“c”}
类A扩展组件{A=“A”}
类B扩展组件{B=“B”}
const res=findObjects(A,B)/[(A的类型未定义)?,(B的类型未定义)?]
对于返回类型,我使用了
Partial
,它是传入类中的一个。如果您的
findObjects
实现过滤掉了相应的类项,则它在索引处包含
undefined
(根据需要调整返回类型)


您可以使用中所述的rest参数


希望有帮助

您可以使用中所述的rest参数


希望有帮助

我的回答如下:

所有类都从一个组件类继承。此函数以某种方式搜索作为这些类实例的对象

因此,在解决方案中,我使用一些基类
组件
,并搜索一些子类成员
子组件

// some empty classes
class Component {}
class SubComponent extends Component {}

function findObjects(...args: Component[]): SubComponent[] { 
  return args.filter(el => el instanceof SubComponent); // do some by instanceof for example
}
// using
const results = findObjects(new Component(), new Component(), new SubComponent());
// results is an array only with SubComponent element

函数的定义如下-
findObjects(…args:Component[]):SubComponent[]

  • …args:Component[]
    -我们将未知数量的组件实例作为参数
  • :SubComponent[]
    -我们返回
    子组件的数组
我建议修改一下它的一点点定义,因为我们返回数组,那么最好也使用数组:

function findObjects(arr: Component[]): SubComponent[] { 
  return arr.filter(el => el instanceof SubComponent); // do some instanceof for example
}
// using
const results = findObjects([new Component(), new Component(), new SubComponent()]);
// results is an array only with SubComponent element

请注意,现在参数是one-
arr
,但它与输出一致,我们放入数组,取出数组。不过只是一个建议。

我的答案如下:

所有类都从一个组件类继承。此函数以某种方式搜索作为这些类实例的对象

因此,在解决方案中,我使用一些基类
组件
,并搜索一些子类成员
子组件

// some empty classes
class Component {}
class SubComponent extends Component {}

function findObjects(...args: Component[]): SubComponent[] { 
  return args.filter(el => el instanceof SubComponent); // do some by instanceof for example
}
// using
const results = findObjects(new Component(), new Component(), new SubComponent());
// results is an array only with SubComponent element

函数的定义如下-
findObjects(…args:Component[]):SubComponent[]

  • …args:Component[]
    -我们将未知数量的组件实例作为参数
  • :SubComponent[]
    -我们返回
    子组件的数组
我建议修改一下它的一点点定义,因为我们返回数组,那么最好也使用数组:

function findObjects(arr: Component[]): SubComponent[] { 
  return arr.filter(el => el instanceof SubComponent); // do some instanceof for example
}
// using
const results = findObjects([new Component(), new Component(), new SubComponent()]);
// results is an array only with SubComponent element

请注意,现在参数是one-
arr
,但它与输出一致,我们放入数组,取出数组。不过这只是一个建议。

[a]
意味着您得到的是1个元素元组,而不是数组。正确的类型是
A[]
谢谢Maciej!更正后的
[A]
表示得到的是1个元素元组,而不是数组。正确的类型是
A[]
谢谢Maciej!修正了,汉克斯!告诉我如何在jsdoc中编写它?像这样尝试:
@param{T扩展{new(…arg:any):any}[]}类@returns{Partial}@template T
。但返回类型显示不正确。@潘克,我实际上从未将JSDoc与TypeScript一起使用过。根据,似乎可以在模板中为
T
添加类型约束。类似于:
@template{new(…arg:any):any}[]}T@param{T}cls@returns{Partial}
谢谢!告诉我如何在jsdoc中编写它?像这样尝试:
@param{T扩展{new(…arg:any):any}[]}类@returns{Partial}@template T
。但返回类型显示不正确。@潘克,我实际上从未将JSDoc与TypeScript一起使用过。根据,似乎可以在模板中为
T
添加类型约束。类似于:
@template{new(…arg:any):any}[]}T@param{T}cls@返回{Partial}