【发布时间】:2020-11-13 22:39:29
【问题描述】:
给定以下代码,当我们调用baz 函数时,typeahead 将显示 'a' 和 'b' 作为可能的值。
但是,如果我想为每个值提供额外的文档,我该怎么做?例如,如果这样的行为是期望的行为:
编辑:
我认为我应该提供更多关于我正在尝试做的事情以及原因的背景信息:
考虑以下示例:
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) => {
...
};
House 是一个 react 组件,它根据颜色和位置道具渲染房子。
这个 House 组件在整个项目中随处可见。
使用当前设置,我可以像这样使用 House:
<House color="Indiana Clay" location="City" />
问题是,智能感知无法识别我作为代码一部分编写的文档:
附:我知道我可以将 paintColor 和位置转换为枚举,并使用这样的东西:
import House, {HouseColor, HouseLocation} from './House';
<House color={HouseColor["Indiana Clay"]} location={HouseLocation.City} />
但是那个组件界面并没有我最初的提议那么好。
【问题讨论】:
-
您正在创建一个由字符串联合组成的类型。您希望如何注释该联合中的每个基本字符串?下面的重载建议非常好。
标签: typescript documentation intellisense jsdoc tsdoc