Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Typescript:是否有方法检查变量是否是具有嵌套属性的接口定义对象?_Javascript_Typescript_Types_Typescript Typings_Typechecking - Fatal编程技术网

Javascript Typescript:是否有方法检查变量是否是具有嵌套属性的接口定义对象?

Javascript Typescript:是否有方法检查变量是否是具有嵌套属性的接口定义对象?,javascript,typescript,types,typescript-typings,typechecking,Javascript,Typescript,Types,Typescript Typings,Typechecking,例如: interface TEST_OBJECT { "test1" : TEST_TYPE; "test2" : TEST_INTERFACE } type TEST_TYPE = string|number; interface TEST_INTERFACE { "test3" : boolean; } 有没有办法检查变量是否是测试对象?如果您正在寻找TypeScript来自动为您提供运行时类型保护,这是不可能的。不过在实践中,编译器非常擅长为您缩小类型范围,

例如:

interface TEST_OBJECT {
    "test1" : TEST_TYPE;
    "test2" : TEST_INTERFACE
}

type TEST_TYPE = string|number;

interface TEST_INTERFACE {
    "test3" : boolean;
}

有没有办法检查变量是否是测试对象?

如果您正在寻找TypeScript来自动为您提供运行时类型保护,这是不可能的。不过在实践中,编译器非常擅长为您缩小类型范围,这取决于您对所检查对象的了解程度。例如,假设您知道某个对象是
测试对象
字符串

function hasKey<K extends string>(k: K, x: any): x is Record<K, {}> {
  return k in x;
}

declare const obj: TEST_OBJECT | string;

if (hasKey("test2", obj)) {
  // it knows that obj is a TEST_OBJECT now
  console.log(obj.test1);
  console.log(obj.test2.test3 === false); 
} else {
  // it knows that obj is a string
  console.log(obj.charAt(0));
}
函数hasKey(k:k,x:any):x是记录{
返回x中的k;
}
declare const obj:TEST|u OBJECT | string;
if(hasKey(“测试2”,obj)){
//它知道obj现在是一个测试对象
console.log(obj.test1);
console.log(obj.test2.test3==false);
}否则{
//它知道obj是一个字符串
控制台日志(对象特征(0));
}
如果你不知道你正在检查的对象可能是什么,那么,正如@JoshCrozier所说的,这可能是一条可行的道路。可以使用与构建接口和类型相同的方式来构建它们:

function hasKey<K extends string>(k: K, x: any): x is Record<K, {}> {
  return k in x;
}

function isTEST_INTERFACE(x: any): x is TEST_INTERFACE {
  return hasKey("test3",x) && (typeof x.test3 === 'boolean');
}

function isTEST_TYPE(x: any): x is TEST_TYPE {
  return (typeof x === 'string') || (typeof x === 'number');
}

function isTEST_OBJECT(x: any): x is TEST_OBJECT {
  return hasKey("test1", x) && isTEST_TYPE(x.test1) &&
    hasKey("test2", x) && isTEST_INTERFACE(x.test2);
}

// use it:
declare const obj: TEST_OBJECT | string
if (isTEST_OBJECT(obj)) {
  console.log(obj.test1);
  console.log(obj.test2.test3 === false); 
} else {
  console.log(obj.charAt(0));
}
函数hasKey(k:k,x:any):x是记录{
返回x中的k;
}
函数isTEST_接口(x:any):x是测试_接口{
返回hasKey(“test3”,x)&(typeof x.test3=='boolean');
}
函数isTEST_类型(x:any):x是测试_类型{
return(typeof x=='string')| |(typeof x=='number');
}
函数isTEST_对象(x:any):x是TEST_对象{
返回hasKey(“test1”,x)和&isTEST_类型(x.test1)&&
hasKey(“test2”,x)和&isTEST_接口(x.test2);
}
//使用它:
声明常量对象:测试对象|字符串
if(isTEST_对象(obj)){
console.log(obj.test1);
console.log(obj.test2.test3==false);
}否则{
控制台日志(对象特征(0));
}

希望这能有所帮助。

除此之外,我不知道还有其他选项,因为无法在运行时检查接口。谢谢,我查看了它,它似乎最适合用于单个顶级属性,如果对象不是由构造函数创建的,则检查的唯一方法是
(variable.test1!==未定义&&variable.test2!==未定义)
。如果是,您可以通过instanceof
If(变量instanceof TEST\u OBJECT)
来选择。