假设我有一个函数,我只想在存在另一个属性时才允许使用某个属性。
我尝试这样做,但它返回错误 Property 'c' does not exist on type 'A'.
type A = {
a?: string;
} & ({ b: string; c?: string } | { b?: string });
const sad = (props: A) => {
const { a, b } = props;
const { c } = props; // Property 'c' does not exist on type 'A'.
return { a, b };
};
Run Code Online (Sandbox Code Playgroud)
有什么解决办法吗?
我有一个项目,我有一个包含机器人对象的游戏文件.游戏文件使用地图保存机器人对象.地图包含机器人的名称作为键,值是机器人对象.
机器人在2D空间中,并且它们具有x,y以找到它们的当前位置.
我必须实现的功能之一是通过查找机器人与原点(0,0)的距离,将机器人从最小到最大排序.
这是我的地图:
std::map<std::string, robot> robot_map;
Run Code Online (Sandbox Code Playgroud)
我使用名称和两个变量初始化机器人以了解位置,并使用第三个变量来查找所采取的步骤总数:
robot::robot(const string &n) : robot_name(n) { x = 0, y = 0, t = 0; }
Run Code Online (Sandbox Code Playgroud)
为了检查机器人与原点的距离我使用:
std::string game::furthest() const
{
int furthest = 0;
std::string max_name;
typedef std::map<std::string, robot>::const_iterator iter;
for (iter p = robot_map.cbegin(); p != robot_map.cend(); ++p) {
if (distance(p->second) > furthest) {
furthest = distance(p->second);
max_name = p->first;
}
}
return max_name;
}
Run Code Online (Sandbox Code Playgroud)
这是距离函数:
int distance(const robot &r) {
int distance;
int y = r.north();
int x = …Run Code Online (Sandbox Code Playgroud)