Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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_Interface_Return - Fatal编程技术网

Typescript:基于输入的返回类型(在接口内)

Typescript:基于输入的返回类型(在接口内),typescript,generics,types,interface,return,Typescript,Generics,Types,Interface,Return,我试图创建一个在接口I3中返回I1的函数,但是类型I1将包含什么?。这取决于字符串的输入。它是I1、I1或I1。我试过使用任何一个和联合体,但都不起作用 也许过载会起作用,但我如何在地狱脸上定义它呢 interface I1<T>{ //methodes } interface I2<T>{ //methodes } type a = { name: string} type b = { licence: string, registred: boolean

我试图创建一个在接口I3中返回I1的函数,但是类型I1将包含什么?。这取决于字符串的输入。它是I1、I1或I1。我试过使用任何一个和联合体,但都不起作用

也许过载会起作用,但我如何在地狱脸上定义它呢

interface I1<T>{
   //methodes
}
interface I2<T>{
   //methodes
}

type a = { name: string}
type b = { licence: string, registred: boolean }
type c = { grade: number  }

interface I3<T,U>{
   something: () => I2,
   notWorking: <k extends T> (...Props:k[]) => I1<?> //not working and ? can be an a,b or c depending on the input
}

我不太明白你想要实现什么,但我认为你可以尝试以下方式:

interface I3<T,U>{
   something: () => I2<U>,
   notWorking: <k extends T> (...Props:k[]) => I1<k extends string ? a : k extends number ? b : c> //not working and ? can be an a,b or c depending on the input
}

注意:您在某物的返回类型中忘记了泛型类型参数

在notWorking中需要一个额外的泛型,Typescript无法知道类型。或者在返回类型中使用条件类型,但如何分配该提取泛型类型?因为我必须用notworking作为例子。可能,但我不想这样做。您如何理解密码类型的使用?返回类型通常基于函数的输入