简体   繁体   English

如何让 typescript 根据参数返回正确的类型

[英]How to let typescript return correct type based on parameters

i'm having a code example like this我有这样的代码示例

interface BaseQuestionType {
  label?: string
}
export type RadioQuestionType = BaseQuestionType & {
  radio: boolean
}
export type TextQuestionType = BaseQuestionType & {
  text: string
}

function test(c: boolean): (RadioQuestionType | TextQuestionType){

  if (c) {
    return {
      radio: true
    }
  } else {
    return {
      text: '11'
    }
  }
}

So you can see i have a function test(c:boolean) that takes in a boolean and give the corresponding return object.所以你可以看到我有一个 function test(c:boolean) ,它接受一个 boolean 并给出相应的返回 object。

The problem is when i use this function, it has error问题是当我使用这个 function 时,它有错误

let a = test(true).radio;

Typescript told me this error in this line: Typescript 在这一行告诉我这个错误:

Property 'radio' does not exist on type 'RadioQuestionType | TextQuestionType'.
  Property 'radio' does not exist on type 'TextQuestionType'.(2339)

As you see, according to the logic ( c is true) , this line of code is correct, how can i tell typescript this?如您所见,根据逻辑( c is true) ,这行代码是正确的,我怎么能告诉 typescript 这个? Or is there any other way to implement this.或者有没有其他方法可以实现这一点。 Thanks谢谢

You need to overload your function to get the desired behaviour like this:您需要重载 function以获得所需的行为,如下所示:

function test(c: true):RadioQuestionType;
function test(c: false):TextQuestionType;
function test(c: boolean): (RadioQuestionType | TextQuestionType){

TSPlayground Link TSPlayground 链接

Similar to Nishant's answer, but using generic :类似于 Nishant 的答案,但使用通用

function test<T extends boolean>(c: T): T extends true ? RadioQuestionType : TextQuestionType;
function test(c: boolean): (RadioQuestionType | TextQuestionType){
   ...
}

let a = test(true).radio;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeScript - 基于多个参数的函数返回类型 - TypeScript - Function return type based on multiple parameters 如何根据 Typescript 中的参数类型声明返回类型 - How to declare return type based on parameter's type in Typescript Typescript:`let result:{[key:T [K]]:T} = {};`不起作用,如何基于泛型键入对象? - Typescript: `let result: { [key: T[K]]: T } = {};` is not working, how can I type my object based on generics? 如何在typescript中输入一个function的参数? - How to type a function with parameters in typescript? Typescript 在 http 请求上设置返回类型的正确方法 - Typescript correct way of setting return type on http requests 使用 typescript 中的通用类型从 object 返回具有正确类型的属性 - Return property from object with correct type using generic typing in typescript 如何让 typescript 类型系统知道返回值的类型? - What can I do to let the typescript type system know the type of the return value? TypeScript:如何将返回类型作为参数的类型? - TypeScript: how to have return type be the type of a parameter? typescript重载类方法 - 返回类型相同,参数不同 - typescript overloading class methods - same return type, different parameters 打字稿:为承诺中的解析参数指定正确的返回类型 - Typescript: Specifying proper return type for resolve parameters in promises
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM