【发布时间】:2022-01-21 17:46:46
【问题描述】:
我有以下代码:
export type EntityType = "foo" | "bar";
export interface Foo {
id: `foo-${string}`;
}
export interface Bar {
id: `bar-${string}`;
}
export type Ent<T extends EntityType> = T extends "foo" ? Foo : Bar;
export const resolve = <T extends EntityType>(id: `${T}-${string}`): Ent<T> => {
if (id.startsWith("foo-")) {
return { id: "foo-a" };
}
if (id.startsWith("bar-")) {
return { id: "bar-a" };
}
throw new Error(`Unsupported entity type ${id}`);
};
return 语句抛出一个错误,上面写着:Type '{ id: "foo-a"; }' is not assignable to type 'Ent<T>'
出现这种情况是因为 TS 无法推断出 .startsWith("foo-") 实际匹配 Foo 接口,这是可以理解的。
有没有办法提示编译器,以便它可以正确推断和缩小预期的返回类型?现在这只有在我投射到任何时才有效:
if (id.startsWith("foo-")) {
return { id: "foo-a" } as any;
}
【问题讨论】:
标签: typescript type-inference template-literals