我有两个联合类型,一个有属性,另一个没有。我假设检查该财产的存在将允许我缩小它,但它不起作用。无法缩小具有未定义属性的简单TypeScript联合类型
我创建了this Playground repro。这其他very similar thing似乎工作得很好。我是否以错误的方式使用工会?
下面的代码为完整起见:
export interface AuthenticatedProfile {
readonly userId: string;
readonly name: string;
}
export interface AnonymousProfile {
readonly userId: undefined;
readonly otherProp: string;
}
export type Profile = AnonymousProfile | AuthenticatedProfile;
function handleProfile(prof: Profile) {
if (prof.userId) {
console.log(prof.name);
}
}
谢谢!
您的解决方案看起来不错。我只有一个问题:如果属性userId是这两种类型的一部分,并且每种类型只能采用不相交的一组值,那么为什么TypeScript在我的原始检查后不能推断这种类型? –
对我来说调用'prof.userId'没有任何类型的警卫工作得很好。 – toskv
它实际上似乎是一个已知的问题。 https://github.com/Microsoft/TypeScript/issues/12600。不知道你为什么看不到这个错误,在操作系统repro中,我发布了 –