【发布时间】:2020-12-18 04:02:05
【问题描述】:
我有一个与user defined type guards 相关的问题,特别是是否可以实现一个谓词函数作为子字段的类型保护。
考虑以下示例:
type ContentFoo = {
kind: "foo";
foo: string;
}
type ContentBar = {
kind: "bar";
bar: string;
}
type Content = ContentFoo | ContentBar;
直接在 content: Content 上定义类型保护就可以了,例如:
function isFooContent(content: Content): content is ContentFoo {
return content.kind === "foo";
}
现在想象Content 被包装成另一种类型,例如:
type Entry = {
id: string;
content: Content;
}
我想要实现的是基于 Entry 包装类型定义类型保护。我天真的猜测是:
function isFooEntry(entry: Entry): entry.content is ContentFoo {
return entry.content.kind === "foo";
}
但是编译器不喜欢这种谓词函数的语法:
Cannot find namespace 'entry'.
有没有其他方法可以编写一个“向上一级”操作的谓词函数?还是因为 TypeScript 编译器无法通过外部类型跟踪类型,所以这样的谓词函数在设计上是不可能的?
预期用途是:
if (isFooEntry(entry)) {
console.log("It must have a foo", entry.content.foo)
}
不用写
if (isFooContent(entry.content)) {
console.log("It must have a foo", entry.content.foo)
}
【问题讨论】:
-
你能提供更多关于你如何使用这个函数的信息吗?
isFooEntry(),我的意思是?
标签: typescript