英文:
Type 'string' is not assignable to type - How to type an object in array?
问题
我有一个长数组:
const allRoles = {
'product_manager': [
{
id: 'productManager_1',
image: '/icon.png',
title: 'CEO of the product',
description: 'Some description.</>',
},
'backend_engineer': [{...}]
...
}
组件代码:
// roleTitle = "Product Manager"
export function OverviewModal(roleTitle: string) {
const convertedRole: keyof typeof allRoles = roleTitle.toLowerCase().replace(/ /g,'_');
const roleCardInfo = allRoles[convertedRole];
// Tried the above but got an error:
// Type 'string' is not assignable to type '"product_manager" | "backend_engineer"...'.ts(2322)
在这种情况下,这似乎不适用:
https://stackoverflow.com/questions/37978528/typescript-type-string-is-not-assignable-to-type
与类不同,我只有一个数组对象。在这种情况下,我不确定它的类型是什么。
英文:
I have a long array:
const allRoles = {
'product_manager': [
{
id: 'productManager_1',
image: '/icon.png',
title: 'CEO of the product',
description: 'Some description'.</>,
},
'backend_engineer': [{...}]
...
}
Component code:
// roleTitle = "Product Manager"
export function OverviewModal(roleTitle: string) {
const convertedRole: keyof typeof allRoles = roleTitle.toLowerCase().replace(/ /g,'_');
const roleCardInfo = allRoles[convertedRole];
// Tried the above but got an error:
// Type 'string' is not assignable to type '"product_manager" | "backend_engineer"...'.ts(2322)
In this case, this doesn't seem to apply:
https://stackoverflow.com/questions/37978528/typescript-type-string-is-not-assignable-to-type
Instead of a class, I just have an object of arrays. I'm not sure what type it would be in this scenario.
答案1
得分: 1
这一部分你实际上在说 convertedRole
应该是 allRoles
的某个键。
const convertedRole: keyof typeof allRoles
allRoles
的类型是你传递给它的值的形状。而你已经声明了你的参数 roleTitle
是一个字符串。所以字符串不是足够狭窄的类型来赋给 convertedRole
。convertedRole
只能被分配等于 allRoles
类型的键的字符串,也就是字符串 "product_manager" | "backend_engineer"...
请记住,TypeScript 不存在于运行时。它无法知道在运行代码时 roleTitle
的实际值。
英文:
So at this part you are literally saying that convertedRole
should be some key of allRoles
.
const convertedRole: keyof typeof allRoles
the type of allRoles
is the shape of the value you are giving it. And you have declared your argument roleTitle
as a string. So a string is not narrow enough a type for convertedRole. convertedRole can only be assigned strings that are equal to the keys of the type of allRoles
, aka the strings "product_manager" | "backend_engineer"...'.
Remember that typescript does not exist in runtime. It can not know the actual value of roleTitle as you run the code.
答案2
得分: 1
Cengen 是对的。
但是,如果您能够在编译时知道 AllRoles 的键,可能会有解决方案,就像这样:
const roleTypeNames = ['product_manager', 'backend_engineer'] as const;
type roleType = typeof roleTypeNames[number]
const allRoles: { [key in roleType]: any } = { ... };
如果是的话,那么您可以使用类型保护。
const isRoleType = (candidate: string): candidate is roleType => {
for (const role of roleTypeNames) {
if (role === candidate) return true;
}
return false;
}
function OverviewModal(roleTitle: string) {
const sanitizedRoleTitle = roleTitle.toLowerCase().replace(/ /g, '_');
if (isRoleType(sanitizedRoleTitle)) {
const roleCardInfo = allRoles[sanitizedRoleTitle];
}
}
英文:
Cengen is right.
But, may you would have a solution to your problem if you were able to know at compile time the key of AllRoles like this :
const roleTypeNames = ['product_manager','backend_engineer'] as const;
type roleType = typeof roleTypeNames[number]
const allRoles : {[key in roleType]:any} = { ... };
if yes, then you can use a typeguard.
const isRoleType = (candidate : string) : candidate is roleType => {
for(const role of roleTypeNames)
{
if(role === candidate) return true ;
}
return false;
}
function OverviewModal(roleTitle: string) {
const sanitizedRoleTitle = roleTitle.toLowerCase().replace(/ /g,'_');
if(isRoleType(sanitizedRoleTitle))
{
const roleCardInfo = allRoles[sanitizedRoleTitle];
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论