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,我最初以为我的问题可以通过有条件的方式解决,但经过进一步检查,似乎歧视性的工会更适合我的情况 非常接近我想要的,但是我仍然不知道如何在我的真实示例中实现它 假设我有一个接口,在这种情况下,它是我使用Passport.js登录后返回的数据: 导出接口用户接口{ 身份证号码 用户名:string 照片:{ 值:字符串 }[] 提供者:“twitter”|“github” _json:TwitterProfileInterface | GithubProfileInterface } 在上述情况下,

我最初以为我的问题可以通过有条件的方式解决,但经过进一步检查,似乎歧视性的工会更适合我的情况

非常接近我想要的,但是我仍然不知道如何在我的真实示例中实现它

假设我有一个接口,在这种情况下,它是我使用Passport.js登录后返回的数据:

导出接口用户接口{
身份证号码
用户名:string
照片:{
值:字符串
}[]
提供者:“twitter”|“github”
_json:TwitterProfileInterface | GithubProfileInterface
}
在上述情况下,
id
username
photos
都可以互换。但是,json的类型取决于提供程序的值。因此,如果
provider
twitter
,那么
\u json
将是
TwitterProfileInterface

有没有办法用Typescript自动完成

编辑:示例代码:

我正在运行switch语句以检查收到的用户配置文件类型:

const user = {
  id: 1,
  username: "User",
  photos: [],
  provider: "twitter",
  _json: {
    // a bunch of data specific to the "twitter" provider
  }
};

switch(user.provider){
  case 'twitter':
    // this case will give types from both TwitterProfileInterface and GithubProfileInterface
    break;
  case 'github':
      // this case will give types from both TwitterProfileInterface and GithubProfileInterface
    break;
}

即使我有一个switch语句,但事实上我用于开关的值包含在
TwitterProfileInterface
TwitterProfileInterface
之外,那么这两种情况都将包括这两种类型,使我的类型变得毫无意义。

使用条件类型和参数,如下所示:

interface TwitterProvider { foo: number }
interface GithubProvider { foo: string }

export type UserInterface<P extends 'twitter' | 'github'> = {
  id: number
  username: string
  photos: {
    values: string
  }[]
  provider: P
  _json: P extends 'twitter' ? TwitterProvider : P extends 'github' ? GithubProvider : {}
}

let foo: UserInterface<'twitter'> = {
  id: 1,
  username: 'foo',
  photos: [{values: 'foo'}],
  provider: 'twitter',
  _json: { foo: 1 }
}
接口TwitterProvider{foo:number}
接口提供程序{foo:string}
导出类型用户界面={
身份证号码
用户名:string
照片:{
值:字符串
}[]
提供者:P
_json:P扩展“twitter”?TwitterProvider:P扩展“github”?GithubProvider:{}
}
让foo:UserInterface={
id:1,
用户名:“foo”,
照片:[{values:'foo'}],
提供商:“twitter”,
_json:{foo:1}
}

我看不出条件类型在这里有多大用处;这看起来更像是您需要一个,例如。这能满足你的需要吗?如果是这样,您是否可以编辑该问题以删除对条件类型的依赖?如果没有,您能否在问题中详细说明这些需求是什么?祝你好运@jcalz这看起来更像我想做的,但是我怎么做才能让类型在我的工会之外决定?对不起,我不明白这个问题。“类型在我的工会之外决定”是什么意思?你能给我举个例子吗?OP中的例子还不够吗?在我的例子中,“switch”(
provider
)不包含在我正在切换的两种类型中,这与Typescript示例不同。因此,虽然我可以将Switch语句与
provider
一起使用,但我不认为这会对我使用的
\u json
类型产生什么影响。这可能对其他人足够了,但对我不够,抱歉。理想情况下,您应该使用所讨论的
switch
语句编写一些示例代码,这样我就可以亲自了解您遇到了什么问题。如果您使用,您在编写什么特定代码时遇到困难?对于接口,这是如何工作的?如果我将其粘贴到PassportUserInterface中,Typescript将抛出错误。什么错误?让我试试看……好吗@八:我编辑了我的答案。如果要添加回退,请将
:{}
更改为
:回退类型
。要添加更多选项,请添加另一个链并更改
extends'…'|'…'
指令。