我有这段代码:
interface MysqlError extends Error {
/**
* Either a MySQL server error (e.g. 'ER_ACCESS_DENIED_ERROR'),
* a node.js error (e.g. 'ECONNREFUSED') or an internal error
* (e.g. 'PROTOCOL_CONNECTION_LOST').
*/
code: string;
}
function f(err: Error | MysqlError) {
if (err.code) {
} else {
}
}
但是出现以下错误:
Property 'code' does not exist on type 'MysqlError | Error'.
Property 'code' does not exist on type 'Error'.
如何检查此功能中使用的类型?
答案 0 :(得分:2)
在这种情况下,我将使用#1:main.Test.TestClass fieldFoo java.util.Map
#2:K
#3:class java.lang.Object
#4:java.lang.Object
#5:sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@ba4d54
#6:class java.lang.Object
#2:V
#3:class java.lang.Object
#4:java.lang.Object
#5:sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@12bc6874
#6:class java.lang.Object
#7:class java.lang.reflect.AccessibleObject
类型防护来测试in
属性的存在:
code