以更 Typescript 的方式解决问题
Solving a problem in a more Typescript way
我正在努力改进我解决这个特定问题的方式
情况。
对于每个请求,我可能会得到一个响应 |事件通知。
处理 Response | 的并集时EventNotification,如果不使用类型转换然后在 if 语句
上分支,我似乎无法解决
示例:
要求:
{
type: 'something', age: 22
}
响应可以是:
type TypeOne = {
Response: { key: 'key', age: 22}
}
或
TypeTwo = {
EventNotification: { key: 'trying to process your request' , age: 33
}
}
然后我有一个函数正在接受这个响应并尝试分支,但是为了使分支工作我必须进行类型转换。
const a = response as TypeOne
const b = response as typeTwo
if(a){
// do stuff here safely.
}else {
// do stuff here safely.
}
我仍然需要检查两者是否存在,但我不想强制转换。
我希望我说清楚了。
另一点可能是:
如何在类型中做到这一点,当一个 属性 被检查为真实时,例如在 if 语句中,另一个 属性 不能被访问?这可能吗?
假设:
Type A = {
KeyOne?: { name: string, age: number},
KeyTwo?: { name: string, height: number}
}
const fn = (a:A) => {
if(a.KeyOne) {
// access OK
// a.KeyTwo doesn't exist
} else if(a.KeyTwo) {
// access OK;
// a.KeyOne doesn't exist type Error;
}
}
那个场景可以用 Typescript 建模吗?
谢谢。
这听起来像是您想要一个 union type which acts as a discriminated union,您可以在其中选中一个 属性 来过滤并集。以下是我的做法:
type A =
{ KeyOne: { name: string, age: number }, KeyTwo?: never } |
{ KeyOne?: never, KeyTwo: { name: string, height: number } }
类型 A
是两种可能的类型之一:具有定义的 KeyOne
属性 和 undefined
KeyTwo
[=32= 的对象](类型为 never
的可选 属性 在您从中读取时将始终为 undefined
),或者具有定义的 KeyTwo
属性 和 undefined
KeyOne
属性。从 TypeScript 3.2 开始,this acts as a discriminated union。您可以看到它按预期工作:
const fn = (a: A) => {
if (a.KeyOne) {
a.KeyOne.age; // okay
a.KeyOne.name; // okay
a.KeyTwo.name; // error! Object is undefined
} else {
a.KeyTwo.name; // okay
a.KeyTwo.height; // okay
a.KeyOne.name; // error! Object is undefined
}
}
看起来不错。好的,希望有所帮助;祝你好运!
我正在努力改进我解决这个特定问题的方式
情况。
对于每个请求,我可能会得到一个响应 |事件通知。
处理 Response | 的并集时EventNotification,如果不使用类型转换然后在 if 语句
上分支,我似乎无法解决示例:
要求:
{
type: 'something', age: 22
}
响应可以是:
type TypeOne = {
Response: { key: 'key', age: 22}
}
或
TypeTwo = {
EventNotification: { key: 'trying to process your request' , age: 33
}
}
然后我有一个函数正在接受这个响应并尝试分支,但是为了使分支工作我必须进行类型转换。
const a = response as TypeOne
const b = response as typeTwo
if(a){
// do stuff here safely.
}else {
// do stuff here safely.
}
我仍然需要检查两者是否存在,但我不想强制转换。 我希望我说清楚了。 另一点可能是: 如何在类型中做到这一点,当一个 属性 被检查为真实时,例如在 if 语句中,另一个 属性 不能被访问?这可能吗?
假设:
Type A = {
KeyOne?: { name: string, age: number},
KeyTwo?: { name: string, height: number}
}
const fn = (a:A) => {
if(a.KeyOne) {
// access OK
// a.KeyTwo doesn't exist
} else if(a.KeyTwo) {
// access OK;
// a.KeyOne doesn't exist type Error;
}
}
那个场景可以用 Typescript 建模吗? 谢谢。
这听起来像是您想要一个 union type which acts as a discriminated union,您可以在其中选中一个 属性 来过滤并集。以下是我的做法:
type A =
{ KeyOne: { name: string, age: number }, KeyTwo?: never } |
{ KeyOne?: never, KeyTwo: { name: string, height: number } }
类型 A
是两种可能的类型之一:具有定义的 KeyOne
属性 和 undefined
KeyTwo
[=32= 的对象](类型为 never
的可选 属性 在您从中读取时将始终为 undefined
),或者具有定义的 KeyTwo
属性 和 undefined
KeyOne
属性。从 TypeScript 3.2 开始,this acts as a discriminated union。您可以看到它按预期工作:
const fn = (a: A) => {
if (a.KeyOne) {
a.KeyOne.age; // okay
a.KeyOne.name; // okay
a.KeyTwo.name; // error! Object is undefined
} else {
a.KeyTwo.name; // okay
a.KeyTwo.height; // okay
a.KeyOne.name; // error! Object is undefined
}
}
看起来不错。好的,希望有所帮助;祝你好运!