我有以下函数,它可能会收到一个未知值:
function formatReason(detail: unknown): string {
if (detail
&& detail instanceof Object
&& detail.constructor.name === 'Object'
&& detail.hasOwnProperty('description')
&& typeof detail['description'] === 'number'
) {
const output = detail['description'];
return output;
}
return '';
}
Run Code Online (Sandbox Code Playgroud)
该detail参数可以是任意值。如果它是一个具有description字符串类型属性的对象,则该函数应返回该属性值,否则返回空字符串。
首先,你建议使用any或unknown用于detail参数?
其次,无论我做什么, for 的类型output最终都是any. 我怎样才能确定它是string?
编辑:被霓虹灯纠正。打字机是不够的。我更新了示例以显式断言unknown值而不是隐式any.
我建议使用,unknown因为它是 的类型安全变体any,也就是说您可能想要使用类型保护来断言未知值。这会导致description您要查找的属性实际上被断言为 astring而不是any。
打字员(见操场看看是什么IDescription):
public hasDescription(obj: unknown): obj is IDescription {
return (obj as IDescription).description !== undefined
&& typeof (obj as IDescription).description === "string";
}
Run Code Online (Sandbox Code Playgroud)
在代码库中使用它会产生一个具有一些好处的 if 语句。
if (this.hasDescription(detail)) {
// In this if-block the TypeScript compiler actually resolved the unknown type to the type described in the guard.
console.log(detail.description);
}
Run Code Online (Sandbox Code Playgroud)
这是一个供您查看其工作原理的游乐场(请注意仅123输出到控制台的方式)。
您的特定问题的示例实现:
function formatReason(detail: unknown): string {
if (this.hasDescription(detail) {
return detail.description;
}
return '';
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7873 次 |
| 最近记录: |