Given the following code, when we call the baz
function, the typeahead will show 'a' and 'b' as possible values.
However, if I want to provide additional documentation for each of those values, how would I do it? For example, if something like this is the desired behavior:
I thought I should give more context about what I'm trying to do and why:
Consider the following example:
const paintColor = {
/** This color is good fo X */
Waltz: "#d9e5d7",
/** This color is good fo Y */
"Indiana Clay": "#ed7c4b",
/** This color is good fo Z */
Odyssey: "#575b6a"
};
const locations = {
/** There are no good school at this location*/
City: "123 city center road",
/** There are lots of parking at this location but it is very far*/
West: "245 some other road"
};
interface HouseProp {
color: keyof typeof paintColor; //"Waltz" | "Indiana Clay" | "Odyssey"
location: keyof typeof locations; //"City" | "West"
}
const House = ({ color, location }: HouseProp) => {
...
};
Where House is a react component that renders a house based on the color and location props.
And this House component is used everywhere throughout the project.
With the current setup, I could use House like this:
<House color="Indiana Clay" location="City" />
The problem is, the IntelliSense can't pick up on the docs I've written as part of the code:
P.S. I know that I could turn paintColor
and locations into enums, and use things like this:
import House, {HouseColor, HouseLocation} from './House';
<House color={HouseColor["Indiana Clay"]} location={HouseLocation.City} />
But that component interface just isn't as nice as my original proposal.
You can't really annotate union members. You can, however, express your union differently — by using overloads or by choosing to use an enum instead.
Solution #1: Function overloads
Screenshot
Solution #2: Overload as an interface
Screenshot
Solution #3: Using an enum instead
Screenshot
I know that's not exactly what you'd like, but that's your second best option.