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
?您定义了一个接口,我已经更新了我的答案,请参见带界面的索引签名
我已经将您的答案添加到书签中。我将回到这个分配中一段时间,非常感谢