Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeScript fails to get the type of the elements of an object correctly

I am developing a React Native project with Expo. I have defined a function called setRoleColors that sets some colors based on the variable role.

export const setRoleColors = (role: string) => {
  switch (role) {
    case "student":
      return { backgroundColor: Color.maroon.dark, color: "#fff" };
    case "moderator":
      return { backgroundColor:"#00ff00", color: "#000"};
    case "user":
      return { backgroundColor:"#0000ff", color: "#fff";
  }
};

I import the function setRoleColors into a component called UserRole.

import React from "react";
import { Text, View } from "react-native";

import { setRoleColors } from "../../services/SetRoleColors";

import styles from "./styles";

const UserRole: React.FC<{ role: string }> = ({ role }) => {
  const { backgroundColor, color } = setRoleColors(role);

  return (
    <View style={{ ...styles.roleContainer, backgroundColor }}>
      <Text style={{ ...styles.roleText, color }}>{role}</Text>
    </View>
  );

Now, everything works perfectly fine, however VS Code underlines the two variables backgroundcolor and color in the line const { backgroundColor, color } = setRoleColors(role); and when I hover over them it shows a message telling me the following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Property 'backgroundColor' does not exist on type'{backgroundColor: string; color: string} | undefined
Property 'color' does not exist on type'{backgroundColor: string; color: string} | undefined 

>Solution :

Look at your setRoleColors:

export const setRoleColors = (role: string) => {
  switch (role) {
    case "student":
      return { backgroundColor: Color.maroon.dark, color: "#fff" };
    case "moderator":
      return { backgroundColor:"#00ff00", color: "#000"};
    case "user":
      return { backgroundColor:"#0000ff", color: "#fff";
  }
};

If you think about it, it’s pretty clear why it might not return an object with a backgroundColor etc property – if role is neither student, nor moderator, nor user. That’s what TypeScript is warning you about.

If you want to indicate that those are the only possible roles, you should make a type for those roles, and indicate that type for role instead of a string.

You could also consider using if/else instead of switch.

type Role = 'student' | 'moderator' | 'user';
export const setRoleColors = (role: Role) => {
    if (role === 'student') {
        return { backgroundColor: Color.maroon.dark, color: "#fff" };
    } else if (role === 'moderator') {
        return { backgroundColor:"#00ff00", color: "#000"};
    } else {
        return { backgroundColor:"#0000ff", color: "#fff";
    }
};

You’ll also need to change UserRole to indicate that the role should be a Role, and not just a string.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading