Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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
Typescript 打字稿:';字符串';can';不能用于索引复杂对象上的类型_Typescript - Fatal编程技术网

Typescript 打字稿:';字符串';can';不能用于索引复杂对象上的类型

Typescript 打字稿:';字符串';can';不能用于索引复杂对象上的类型,typescript,Typescript,我有一个复杂的物体 const classes = { Barbarian: { armorAndWeoponProficianies: [ 'lightAmour', 'mediumArmour', 'Sheilds', 'simpleWeopons', 'martialWeopons', ], class: 'Barbarian', description: 'A fierce war

我有一个复杂的物体

const classes = {
  Barbarian: {
    armorAndWeoponProficianies: [
      'lightAmour',
      'mediumArmour',
      'Sheilds',
      'simpleWeopons',
      'martialWeopons',
    ],
    class: 'Barbarian',
    description:
      'A fierce warrior of primitive background who can enter a battle rage',
    hitDie: '1d12',
    primaryAbility: 'STR',
    savingThrowProficianies: ['STR', 'CON'],
  },
  Bard: {
    armorAndWeoponProficianies: [
      'lightArmor',
      'simpleWeapons',
      'handCrossbows',
      'longswords',
      'rapiers',
      'shortswords',
    ],
    class: 'Bard',
    description:
      'An inspiring magician whose power echoes the music of creation',
    hitDie: '1d8',
    primaryAbility: 'CHA',
    savingThrowProficianies: ['DEX', 'CHA'],
  },
}
这里我想使用参数classType作为键,但是TypeScript不喜欢它

  const hitPointGenerator = (classType: string) => {
    const selectedClass = classes[classType].hitDie;
//                                  ^ Error
    console.log(selectedClass)
  };
我得到的错误是

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ Barbarian: { armorAndWeoponProficianies: string[]; class: string; description: string; hitDie: string; primaryAbility: string; savingThrowProficianies: string[]; }; Bard: { armorAndWeoponProficianies: string[]; ... 4 more ...; savingThrowProficianies: string[]; }; ... 9 more ...; Wizzard: { ...; }; }'.
  No index signature with a parameter of type 'string' was found on type '{ Barbarian: { armorAndWeoponProficianies: string[]; class: string; description: string; hitDie: string; primaryAbility: string; savingThrowProficianies: string[]; }; Bard: { armorAndWeoponProficianies: string[]; ... 4 more ...; savingThrowProficianies: string[]; }; ... 9 more ...; Wizzard: { ...; }; }'.ts(7053)

之所以会出现这种情况,是因为
只有两个有效键,但字符串可以是任何字符串

也许可以尝试将函数签名更改为

const hitPointGenerator = (classType: keyof typeof classes) => {
    const selectedClass = classes[classType].hitDie;
    console.log(selectedClass)
  };

因此,只有
“Barbarian”|“Bard”
是类类型的有效输入,这是因为
类只有两个有效键,但字符串可以是任何字符串

也许可以尝试将函数签名更改为

const hitPointGenerator = (classType: keyof typeof classes) => {
    const selectedClass = classes[classType].hitDie;
    console.log(selectedClass)
  };

因此,只有
“Barbarian”|“Bard”
是类类型的有效输入

有不同的方法来解决这个问题:

使用正确的类型 将
classType
参数更改为
keyof-typeof-classes

const hitPointGenerator = (classType: keyof typeof classes) => {
    const selectedClass = classes[classType].hitDie;
    console.log(selectedClass)
};
这样做的好处是,您不能意外地用错误的键调用它,例如

hitPointGenerator('Bard'); // OK
hitPointGenerator('Bart'); // compile error - notice the 't' at the end

将鼠标悬停在typescript中的
类类型
参数上时,可以看到类型是
“Barbarian”|“Beard”
:这是一个,在本例中是
字符串
的子类型(即,它更具体)

使用字符串索引签名

使用此字符串索引签名,您可以将新项目动态添加到
classes
对象:

classes['new'] = {
  ...
}
更多关于

带接口的索引签名 要同时使用值的类型,您可以定义一个接口,例如:

interface IMember {
  armorAndWeoponProficianies: string[];
  class: string;
  description: string;
  hitDie: string;
  primaryAbility: string;
  savingThrowProficianies: string[];
}

// we use an index signature to tell typescript that keys of type string are allowed
const classes: {
  [key: string]: IMember;
} = {
...
}

投给任何人 这只是为了完成:我会避免这样

禁用“noImplicitAny”` 禁用
tsconfig.json中的
noImplicitAny
选项

当您将一些javascript代码迁移到typescript时,这会很有用:我不会在新项目中使用它,只要有可能,就尝试激活
noImplicitAny


有不同的方法来解决这个问题:

使用正确的类型 将
classType
参数更改为
keyof-typeof-classes

const hitPointGenerator = (classType: keyof typeof classes) => {
    const selectedClass = classes[classType].hitDie;
    console.log(selectedClass)
};
这样做的好处是,您不能意外地用错误的键调用它,例如

hitPointGenerator('Bard'); // OK
hitPointGenerator('Bart'); // compile error - notice the 't' at the end

将鼠标悬停在typescript中的
类类型
参数上时,可以看到类型是
“Barbarian”|“Beard”
:这是一个,在本例中是
字符串
的子类型(即,它更具体)

使用字符串索引签名

使用此字符串索引签名,您可以将新项目动态添加到
classes
对象:

classes['new'] = {
  ...
}
更多关于

带接口的索引签名 要同时使用值的类型,您可以定义一个接口,例如:

interface IMember {
  armorAndWeoponProficianies: string[];
  class: string;
  description: string;
  hitDie: string;
  primaryAbility: string;
  savingThrowProficianies: string[];
}

// we use an index signature to tell typescript that keys of type string are allowed
const classes: {
  [key: string]: IMember;
} = {
...
}

投给任何人 这只是为了完成:我会避免这样

禁用“noImplicitAny”` 禁用
tsconfig.json中的
noImplicitAny
选项

当您将一些javascript代码迁移到typescript时,这会很有用:我不会在新项目中使用它,只要有可能,就尝试激活
noImplicitAny


ben您的解决方案效果很好,您能推荐任何我能更了解这一点的阅读吗?我不断遇到这个索引问题。如果您的解决方案效果很好,您能推荐任何我能更了解这一点的阅读吗?我不断遇到这个索引问题使用字符串索引签名,我如何避免使用
any
?您定义了一个接口,我已经更新了我的答案,请参阅
index signature with interface
我已经将您的答案添加到书签中我将回到这个分配中一段时间,非常感谢您使用字符串索引签名,我如何避免使用
any
?您定义了一个接口,我已经更新了我的答案,请参见
带界面的索引签名
我已经将您的答案添加到书签中。我将回到这个分配中一段时间,非常感谢