考虑我们有一个下面定义的/常量对象,我们想将其键用作类型;
const componentDetail = {
"ejareRaf" : {
text : "rented",
background : "linear-gradient(0deg, rgba(243, 7, 27, 0.6), rgba(243, 7, 27, 0.6));"
},
"hamahangBazdid" : {
text : "visit-set",
background : "rgba(13, 81, 55, 0.6)"
},
"onMozakere" : {
text : "negotitaing",
background : "linear-gradient(0deg, rgba(217, 171, 33, 0.6), rgba(217, 171, 33, 0.6));"
},
"dideShod" : {
text : "seen",
background : "rgba(18, 25, 33, 0.4)"
},
"cancelBazdid" : {
text : "canceled",
background : "rgba(238, 45, 123, 0.3)"
},
}
现在我要从上面的对象键推导出一个type
,像这样:
"ejareRaf" | "hamahangBazdid" | "onMozakere" | "dideShod" | "cancelBazdid"
答案 0 :(得分:0)
经过几次尝试和错误后,我发现它是myslef:
type componentDetailKeys = keyof typeof componentDetail;
// "ejareRaf" | "hamahangBazdid" | "onMozakere" | "dideShod" | "cancelBazdid"