我希望新的TypeScript 3.7功能可以支持以下情况:
export interface Foo {
aaa: string;
bbb: string;
ccc: string;
}
export interface Bar {
ddd: string;
eee: string;
fff: string;
}
type Data = Foo | Bar;
const doSomething = (data: Data) => {
const optionalChaining = data?.aaa //error
const nullishCoalescing = data.bbb ?? "default"; //error
if ("ccc" in data) {
//OK this works
}
}
除了后者在较早的ts版本中也起作用之外,是否还有其他方法可以处理此问题? ts-playground:http://www.typescriptlang.org/play/?ts=3.7-Beta#code/KYDwDg9gTgLgBASwHY2FAZgQwMbDgMQgjgG8BYAKDjk1oC44BnGKZAcwG5LqAjPh5qySducbOIEt2XCgF9KlUJFiIUaLLjgAhTFFKiAJkclCRVOMEsnpo9HevCZ8ipRgBPMHgAimGJjgAvAREcAA+2roylNgQSMxwBhAAyhAAtsAwABbsgXAAFAa+mAw+fgCUgQB8+uYxcfAQYDAIsZgANgDCmZjIOUGFfgD8AHS0mKJ18UgArm1tCIyZHRDtwIzYfQlFw3w8cIODcABEBsBYszBHUeYI6PlH4thHqlvlNdTUAPSfAPIA0nAsgs4AB3aAAa0YomcziAA