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 - Fatal编程技术网

如何设置类型取决于typescript中的函数参数?

如何设置类型取决于typescript中的函数参数?,typescript,Typescript,我的测试代码 type Config = { id: string; password: string; } type TrueTypeConfig = Config & { emailId: number; paymentId: number; } type FalseTypeConfig = Config & { documentId: number; buyerId: number; } type GetTestConfig<T exte

我的测试代码

type Config = {
  id: string;
  password: string;
}

type TrueTypeConfig = Config & {
  emailId: number;
  paymentId: number;
}

type FalseTypeConfig = Config & {
  documentId: number;
  buyerId: number;
}

type GetTestConfig<T extends boolean> = (type: T, id: string, password: string)
  => T extends true
    ? TrueTypeConfig
    : FalseTypeConfig
    
function getTestConfig<T extends boolean> (type: T, id: string, password: string): T extends true ? TrueTypeConfig : FalseTypeConfig;
function getTestConfig (type: boolean, id: string, password: string): TrueTypeConfig | FalseTypeConfig {
  const config: TrueTypeConfig | FalseTypeConfig = {
    id: getRandomData(id),
    password: getRandomData(password),
  };
  if (type) {
    config.emailId = getRandomNumber(1, 100);
    config.paymentId = getRandomNumber(1, 100);
  } else {
    config.documentId = getRandomNumber(1, 100);
    config.buyerId = getRandomNumber(1, 100);
  }
  return config;
};

const res1 = getTestConfig(false, '1', '1'); // FalseTypeConfig
const res2 = getTestConfig(true, '1', '1'); // TrueTypeConfig
类型配置={
id:字符串;
密码:字符串;
}
键入TrueTypeConfig=Config&{
emailId:number;
paymentId:编号;
}
输入FalseTypeConfig=Config&{
documentId:编号;
buyerId:数字;
}
类型GetTestConfig=(类型:T,id:string,密码:string)
=>T扩展为真
? TrueTypeConfig
:FalseTypeConfig
函数getTestConfig(type:T,id:string,password:string):T扩展了true?TrueTypeConfig:FalseTypeConfig;
函数getTestConfig(类型:boolean,id:string,密码:string):TrueTypeConfig | FalseTypeConfig{
常量配置:TrueTypeConfig | FalseTypeConfig={
id:getRandomData(id),
密码:getRandomData(密码),
};
如果(类型){
config.emailId=getRandomNumber(1100);
config.paymentId=getRandomNumber(1100);
}否则{
config.documentId=getRandomNumber(1100);
config.buyerId=getRandomNumber(1100);
}
返回配置;
};
const res1=getTestConfig(false,'1','1');//FalseTypeConfig
const res2=getTestConfig(true,'1','1');//TrueTypeConfig
“getTestconfig”函数必须返回ConfigType True或False配置。它取决于参数“类型”。(正在使用上述测试代码) 但是
const-config:TrueTypeConfig | FalseTypeConfig
是类型错误,也在if/else代码中

我的问题

  • 当set依赖于参数时,上面的测试代码是否是函数返回类型的常用方法
  • 我不知道如何解决类型问题
    const-config:TrueTypeConfig | FalseTypeConfig
我想知道更好的方法“如何定义返回类型”

我认为使用泛型这种方法作为函数返回非常好。正如您所注意到的,编译器很遗憾无法验证函数实现中的特定值是否可分配给依赖于未指定泛型类型参数的条件类型;这是本书的主题。除非这个问题得到解决,否则您必须将类型安全的一些负担从编译器转移到您身上,比如使用一个调用签名,或者像您所做的那样,使用一个调用签名


至于
getTestConfig()
的实现,不能说
config
TrueTypeConfig | FalseTypeConfig
,因为它没有足够的属性开始。充其量你可以说它的类型是
Config
,然后再把它缩小到所需的类型之一

与其分阶段初始化
config
,编译器很难验证这一点,我只需要使用类似或的东西一次初始化
config

function getTestConfig<T extends boolean>(type: T, id: string, password: string): T extends true ? TrueTypeConfig : FalseTypeConfig;
function getTestConfig(type: boolean, id: string, password: string): TrueTypeConfig | FalseTypeConfig {
  return {
    id: getRandomData(id),
    password: getRandomData(password),
    ...(type ?
      {
        emailId: getRandomNumber(1, 100),
        paymentId: getRandomNumber(1, 100)
      } : {
        documentId: getRandomNumber(1, 100),
        buyerId: getRandomNumber(1, 100)
      }
    )
  };
}
函数getTestConfig(type:T,id:string,password:string):T扩展为true?TrueTypeConfig:FalseTypeConfig; 函数getTestConfig(类型:boolean,id:string,密码:string):TrueTypeConfig | FalseTypeConfig{ 返回{ id:getRandomData(id), 密码:getRandomData(密码), …(类型? { emailId:getRandomNumber(1100), paymentId:getRandomNumber(1100) } : { documentId:getRandomNumber(1100), buyerId:getRandomNumber(1100) } ) }; } 这应该编译得很干净


我能想象的唯一另一种更安全地执行依赖于输入参数的返回类型(完全排除重载)的方法是尝试对返回使用。我试过这个,它真的很难看/不可穿透,并且依赖于通过TS4.1将
boolean
值强制为其等效的
string
。我甚至不会在本文中包含代码,因为我不推荐它。如果你真的在意,它包含在下面的游乐场链接中,如果你愿意,我很乐意解释。但我认为泛型条件类型可能是目前最好的解决方案


我认为以这种方式使用泛型函数作为函数返回非常好。正如您所注意到的,编译器很遗憾无法验证函数实现中的特定值是否可分配给依赖于未指定泛型类型参数的条件类型;这是本书的主题。除非这个问题得到解决,否则您必须将类型安全的一些负担从编译器转移到您身上,比如使用一个调用签名,或者像您所做的那样,使用一个调用签名


至于
getTestConfig()
的实现,不能说
config
TrueTypeConfig | FalseTypeConfig
,因为它没有足够的属性开始。充其量你可以说它的类型是
Config
,然后再把它缩小到所需的类型之一

与其分阶段初始化
config
,编译器很难验证这一点,我只需要使用类似或的东西一次初始化
config

function getTestConfig<T extends boolean>(type: T, id: string, password: string): T extends true ? TrueTypeConfig : FalseTypeConfig;
function getTestConfig(type: boolean, id: string, password: string): TrueTypeConfig | FalseTypeConfig {
  return {
    id: getRandomData(id),
    password: getRandomData(password),
    ...(type ?
      {
        emailId: getRandomNumber(1, 100),
        paymentId: getRandomNumber(1, 100)
      } : {
        documentId: getRandomNumber(1, 100),
        buyerId: getRandomNumber(1, 100)
      }
    )
  };
}
函数getTestConfig(type:T,id:string,password:string):T扩展为true?TrueTypeConfig:FalseTypeConfig; 函数getTestConfig(类型:boolean,id:string,密码:string):TrueTypeConfig | FalseTypeConfig{ 返回{ id:getRandomData(id), 密码:getRandomData(密码), …(类型? { emailId:getRandomNumber(1100), paymentId:getRandomNumber(1100) } : { documentId:getRandomNumber(1100), buyerId:getRandomNumber(1100) } ) }; } 这应该编译得很干净


我能想象的唯一另一种更安全地执行依赖于输入参数的返回类型(完全排除重载)的方法是尝试对返回使用。我试过这个,它真的很难看/不可穿透,并且依赖于通过TS4.1将
boolean
值强制为其等效的
string
。我甚至不会在本文中包含代码,因为我不推荐它。它包含在游戏组中