Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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_Typescript2.0_Typescript Types - Fatal编程技术网

如何正确声明以下typescript接口/类型?

如何正确声明以下typescript接口/类型?,typescript,typescript2.0,typescript-types,Typescript,Typescript2.0,Typescript Types,我正在用Typescript构建Apollo GraphQL服务器,在理解类型系统中处理事情的正确方法时遇到了问题。尽管GraphQL和Apollo是代码的一部分,但我想知道TypeScript部分的具体情况。我也很难理解接口与类型的角色,以及每种类型的最佳实践是什么(例如,何时使用类型与接口,如何处理扩展,等等) 我的大部分不一致性都在解析器中。我将在代码中的相关部分旁边留下注释和问题。再次感谢您提供的任何帮助: 银行账户类型={ id:字符串; 类型:字符串; 属性:SpendingAcc

我正在用Typescript构建Apollo GraphQL服务器,在理解类型系统中处理事情的正确方法时遇到了问题。尽管GraphQL和Apollo是代码的一部分,但我想知道TypeScript部分的具体情况。我也很难理解接口与类型的角色,以及每种类型的最佳实践是什么(例如,何时使用类型与接口,如何处理扩展,等等)

我的大部分不一致性都在解析器中。我将在代码中的相关部分旁边留下注释和问题。再次感谢您提供的任何帮助:


银行账户类型={
id:字符串;
类型:字符串;
属性:SpendingAccountAttributes | SavingsAccountAttributes
}
//我意识到这种“消费账户属性|储蓄账户属性”的做法是错误的
//做我想做的事。我基本上是想告诉你
//键入系统,使其可以是一个或另一个。据我所知,它的书写方式
//将创建一个联合,仅返回这两种类型之间共享的字段
//案例本质上属于“BankingAttributes”类型的内容。对吗?
接口银行属性={
路由编号:字符串;
帐号:字符串;
余额:数字;
fundsAvailable:编号;
}
//是否最好删除特定于“SpendingAccountAttribute”和“SavingsAccountAttribute”的属性
//类型,并将其作为可选类型保留在“BankingAttributes”上。我会的
//及时地为“SpendingAccount”和“SavingAccount”创建一个独立的解析器
//查询,因此拥有它们似乎很有用。不过不确定
接口SpendingAccountAttribute扩展了BankingAttribute{
defaultPaymentCardId:字符串;
defaultPaymentCardLastFour:字符串;
会计特征:记录;
}
接口SavingsAccountAttributes扩展银行属性{
利率:数字;
利息TD:数字;
}
//混合类型和接口看起来很混乱。应该是哪一个?如果是“类型”,我该怎么做
//将“BankingAttributes”扩展为“spendingaccountributes”,告诉类型系统
//这些应该是SpendingAccount属性的一部分吗?
导出默认值{
查询:{
bankingAccounts:async(_source:string,_args:[],{dataSources}:Record):Promise=>{
//以下内容为'accounts'API路由创建了一个restful API,我们将类型作为'includes'传入,即'API/v2/accounts?types[]=支出和类型[]=储蓄
const accounts=await.dataSources.api.getAccounts(['spending','savings'])
常量响应=accounts.data.map((acc:BankingAccount)=>{
const{fundsAvailable,accountFeatures,…other}=acc.attributes
返回{
id:acc.id,
型号:acc.type,
可支取余额:可支取资金,
//accountFeatures编译失败,出现以下错误:
//“类型“SpendingAccountAttribute | SavingsAccountAttributes”上不存在accountFeatures”
//处理这个问题的最好方法是什么,这样我就可以提取accountFeatures了
//对于支出帐户(这是该属性将显示的唯一帐户类型)?
会计特征,
……其他
}
})
返回响应
}
}
}

我的经验法则是在可能的情况下使用
接口。基本上,无论何时处理具有已知键(值可以是复杂类型)的对象,都可以使用接口。因此,您可以将
银行账户
设为
接口

您设置消费和储蓄账户以扩展共享界面的方式非常棒

当你有一个
银行账户时,你知道它有支出或储蓄属性,但你不知道是哪个属性

一个选项是检查它使用的是哪种类型

另一个选项是定义一个附加的
类型
,该类型具有两种帐户类型的所有属性,但为可选

type CombinedAttributes = Partial<SpendingAccountAttributes> & Partial<SavingsAccountAttributes>
在输入所有这些之后…我意识到
BankingAccount
有一个
类型
。该类型是“支出”还是“储蓄”?在这种情况下,我们还想在
类型
属性
之间建立一个链接

BankingAccount
类型定义应允许您访问任一类型的属性,同时还允许您仅通过检查
type
的值来将帐户缩小为储蓄或支出。由于这里的联合,这必须是
类型
而不是
接口
,但这不是真实的是的

type BankingAccount = {
    id: string;
    attributes: CombinedAttributes;
} & ({
    type: "savings";
    attributes: SavingsAccountAtrributes;
} | {
    type: "spending";
    attributes: SpendingAccountAttributes;
}) 

function myFunc( account: BankingAccount ) {

    // interestRate might be undefined because we haven't narrowed the type
    const interestRate: number | undefined = account.attributes.interestRate;

    if ( account.type === "savings" ) {
        // interestRate is known to be a number on a savings account
        const rate: number = account.attributes.interestRate
    }
}

请将您的帖子拆分为多个问题,每篇帖子一个问题。@DanielRearden我已经删除了所有与类型或接口无关的问题。剩下的问题我就不提了,因为它们关系密切。
部分的
正是我要找的。谢谢!而且,这两个问题的类型都是
BankingAccount
以区别于其他类型(如InvestmentAccount)。
type BankingAccount = {
    id: string;
    attributes: CombinedAttributes;
} & ({
    type: "savings";
    attributes: SavingsAccountAtrributes;
} | {
    type: "spending";
    attributes: SpendingAccountAttributes;
}) 

function myFunc( account: BankingAccount ) {

    // interestRate might be undefined because we haven't narrowed the type
    const interestRate: number | undefined = account.attributes.interestRate;

    if ( account.type === "savings" ) {
        // interestRate is known to be a number on a savings account
        const rate: number = account.attributes.interestRate
    }
}