Hone logo
Hone
Problems

Defining Props with TypeScript in Vue 3

This challenge focuses on correctly defining and validating props for a Vue 3 component using TypeScript. Properly defining props ensures type safety, improves code readability, and helps prevent errors during development. This is a fundamental skill for building robust and maintainable Vue applications.

Problem Description

You are tasked with creating a Vue 3 component called UserCard that displays user information. The component should accept the following props:

  • name: A string representing the user's name. This prop is required.
  • age: A number representing the user's age. This prop is optional.
  • isActive: A boolean indicating whether the user is active. This prop is optional and defaults to true.
  • occupation: A string representing the user's occupation. This prop should be one of the following values: "Engineer", "Doctor", "Teacher", or "Student". This prop is optional.

You need to define these props using TypeScript within the Vue component's defineProps API. The prop definitions should include:

  • Type annotations for each prop.
  • Whether each prop is required or optional.
  • A default value for the isActive prop.
  • A validator for the occupation prop to ensure it's one of the allowed values.

Examples

Example 1:

Input:  Component usage: <UserCard name="Alice" age={30} occupation="Engineer" />
Output: The component renders a card displaying "Name: Alice", "Age: 30", "Is Active: true", and "Occupation: Engineer".
Explanation: All required props are provided, and the optional props have their default values or are explicitly set. The occupation is a valid value.

Example 2:

Input: Component usage: <UserCard name="Bob" />
Output: The component renders a card displaying "Name: Bob", "Age: undefined", "Is Active: true", and "Occupation: undefined".
Explanation: Only the required `name` prop is provided. The optional props take their default values.

Example 3:

Input: Component usage: <UserCard name="Charlie" occupation="Chef" />
Output:  A warning is displayed in the console indicating that the `occupation` prop is invalid. The component renders with `occupation` as `undefined`.
Explanation: The `occupation` prop is provided with an invalid value ("Chef"). The validator prevents the component from using this invalid value.

Constraints

  • The solution must use Vue 3's defineProps API.
  • The solution must be written in TypeScript.
  • The validator for occupation must use a type-safe approach (e.g., a union type).
  • The component should not render any content if the name prop is not provided. This is an edge case to consider.

Notes

  • Consider using a union type to define the allowed values for the occupation prop.
  • The validator should prevent the component from using invalid values for the occupation prop. It doesn't need to throw an error, but should prevent the invalid value from being used.
  • Focus on type safety and clear prop definitions.
  • You don't need to implement the actual rendering logic of the UserCard component; only the prop definitions are required. A simple console.log statement to verify the prop values is sufficient.
Loading editor...
typescript