使用以下代码编译tsc --strictNullChecks失败error TS2339: Property 'name' does not exist on type '{}'.
type Obj = {} | undefined;
type User = {
email: string;
password: string;
name: string;
};
type PartialUser = Partial<User>;
function isUser(obj: Obj): obj is PartialUser {
return true;
}
function getUserName(obj: Obj) {
if (isUser(obj)) {
return obj.name;
}
return '';
}
Run Code Online (Sandbox Code Playgroud)
不过,如果我更换type PartialUser = Partial<User>;与
type PartialUser = {
email?: string;
password?: string;
name?: string;
};
Run Code Online (Sandbox Code Playgroud)
一切都很笨拙.
有一些可用的解决方法,但我很好奇为什么会这样.PartialUser的这两个定义不应该在功能上等同吗?我在版本3.1.3
| 归档时间: |
|
| 查看次数: |
132 次 |
| 最近记录: |