Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.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 - Fatal编程技术网

Javascript typescript动态检查字符串是否为复杂对象的属性

Javascript typescript动态检查字符串是否为复杂对象的属性,javascript,typescript,Javascript,Typescript,我真的需要能够动态地检查aa是否是名为validations的复杂对象的属性 const check = 'propertya' + 'a'; // result of a complex calculation that returns string 'propertyaa' validations= { 'a' : { 'propertyaa': 'ax', 'ab': 'ay', 'ac': 'az' }, 'b' : {

我真的需要能够动态地检查aa是否是名为validations的复杂对象的属性

const check = 'propertya' + 'a'; // result of a complex calculation that returns string 'propertyaa'

validations= {
    'a' : {
      'propertyaa': 'ax',
      'ab': 'ay',
      'ac': 'az'
    },
    'b' : {
      'ba': 'ax',
      'bb': 'ay',
      'bc': 'az'
    }
  };

if (this.validations.a[check] === undefined) {  ...
错误是:

element implicitly has an any type because type '{ ' propertyaa': string, 'ab': string, 'ac': string; }' has no index signature
(property) 'a': {
    'propertyaa': string;
    'ab': string;
    'ac': string;
}

奇怪的是,如果(this.validations.a['ab']){

您可以选择两种方法中的一种,那么静态(非动态)变量就可以工作。您可以向验证属性添加索引签名:

type Indexable = { [name: string]: string }
const validations = {
    'a': {
        'propertyaa': 'ax',
        'ab': 'ay',
        'ac': 'az'
    } as Indexable,
    'b': {
        'ba': 'ax',
        'bb': 'ay',
        'bc': 'az'
    } as Indexable
};

const check = 'propertya' + 'a';
if (validations.a[check] === undefined) {
}
您也可以在使用属性时将其强制转换为可索引的:

if ((validations.a as Indexable)[check] === undefined) {
}
您也可以使用
any
而不是
Indexable
,但是上面定义的
Indexable
any
提供了更多的类型安全性

另一个选项是断言字符串
check
实际上是
a
值的键:

if (validations.a[check as 'propertyaa'] === undefined) {
}
编辑

如果使用第一个选项,则helper函数可以帮助避免对
验证的每个属性进行类型断言:

type Indexable = { [name: string]: string }
const validations = createValidations({
    'a': {
        'propertyaa': 'ax',
        'ab': '19',
        'ac': 'az'
    },
    'b': {
        'ba': 'ax',
        'bb': 'ay',
        'bc': 'az'
    }
});
function createValidations<T>(o: { [P in keyof T]: T[P] & Indexable}) : typeof o {
    return o;
}
const check = 'propertya' + 'a';
if (validations.a[check] === undefined) {
}
type Indexable={[name:string]:string}
const validations=createValidations({
“a”:{
“财产YAA”:“ax”,
‘ab’:‘19’,
“ac”:“az”
},
“b”:{
‘ba’:‘ax’,
‘bb’:‘ay’,
“bc”:“az”
}
});
函数createValidations(o:{[P in keyof T]:T[P]&可索引}):类型o{
返回o;
}
常量检查='propertya'+'a';
if(validations.a[检查]==未定义){
}
您需要


有一个更简单的解决方案,也是迄今为止唯一对我有效的解决方案:

cartoons = {
      'producers': {
          'Disney': 'Walt Disney',
          'PIXAR1': 'John John'
       }
    }

const check = 'Dis' + 'ney';
    const y = (<any>this.cartoons.producers)[check]; // notice the cast to any
    alert(y);
卡通={
“制作人”:{
“迪斯尼”:“沃尔特·迪斯尼”,
“皮克斯1”:“约翰·约翰”
}
}
常量检查='Dis'+'ney';
const y=(this.cartoons.producers)[检查];//注意任何
警报(y);

这两个测试对我来说都很好…这个位给出了错误:
这个.validations.a[检查]
?我无法复制这个。我的测试工作正常。是的,那个位:这个.validations.a[检查]给出错误抱歉,但我得到:元素显式具有any类型,因为类型string | ComplexObject没有索引签名。我是否遗漏了什么?谢谢!现在,即使我检查了此项,我也会得到它。验证。a['propertyaa']
cartoons = {
      'producers': {
          'Disney': 'Walt Disney',
          'PIXAR1': 'John John'
       }
    }

const check = 'Dis' + 'ney';
    const y = (<any>this.cartoons.producers)[check]; // notice the cast to any
    alert(y);