我正在將ReactJS撰寫的應用程式轉換JS為Typescript.
我收到此錯誤:
元素隱式具有“any”型別,因為“string”型別的運算式不能用于索引“topBarType”型別。在型別“topBarType”上找不到帶有“string”型別引數的索引簽名。
type topBarType = {
height: string;
"font-size": string;
iconWidth: string;
iconHeight: string;
};
type variantTypes = 'sm'|'md'|'lg';
export function topBarTheme(variant: variantTypes, property: string): string {
const theme: { [key: string]: topBarType } = {
sm: {
height: `height:2rem;`,
"font-size": `font-size: 1.5rem;`,
iconWidth: `width: 1.75rem;`,
iconHeight: `height: 1.75rem;`,
},
md: {
height: `height:2.5rem;`,
"font-size": `font-size: 1.75rem;`,
iconWidth: `width: 2rem;`,
iconHeight: `height: 2rem;`,
},
lg: {
height: `height:3rem;`,
"font-size": `font-size: 2rem;`,
iconWidth: `width: 2.5rem;`,
iconHeight: `height: 2.5rem;`,
},
};
if (!variant) return theme["lg"][property]; <-- Error
return theme[variant][property]; <-- Error
}
uj5u.com熱心網友回復:
嘗試定義property引數的型別:
type topBarProperties = keyof topBarType;
export function topBarTheme(
variant: variantTypes,
property: topBarProperties
): string {
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/368244.html
標籤:javascript 反应 打字稿
