gpt4 book ai didi

typescript type is not assignable to type `symbol` 错误

转载 作者:行者123 更新时间:2023-12-05 07:10:16 24 4
gpt4 key购买 nike

export type anchorPositionType = "middle" | "left" | "right" | "top" | "bottom" | customAnchorType;
export type customAnchorType = { rightness?: number; bottomness?: number };

const getAnchorOffset = (width:number, height:number): { [key in anchorPositionType]: customAnchorType } => // here 'anchorPositionType' getting the error Type 'customAnchorType' is not assignable to type 'symbol' and i don't understand why?
{
return {
middle: { rightness: width * 0.5, bottomness: height * 0.5 },
left: { rightness: 0, bottomness: height * 0.5 },
right: { rightness: width, bottomness: height * 0.5 },
top: { rightness: width * 0.5, bottomness: 0 },
bottom: { rightness: width, bottomness: height * 0.5 }
};
};

我不知道是什么问题。为什么函数签名处的 anchorPositionType 出现错误 Type 'customAnchorType' is not assignable to type 'symbol'

最佳答案

好吧,在我的例子中,编译器提示 customAnchorType 不是联合类型,因此不能对他使用 in 运算符。

type anchorPositionType = 'middle' | 'left' | 'right' | 'top' | 'bottom' | customAnchorType;
type customAnchorType = { rightness?: number; bottomness?: number };
type test = { [key in anchorPositionType]: customAnchorType };
^
Type 'customAnchorType' is not assignable to type 'symbol'.

('customAnchorType' is an object)

如果我们要从 anchorPositionType 中移除 customAnchorType:

type anchorPositionType = 'middle' | 'left' | 'right' | 'top' | 'bottom' 

错误会消失,因为现在 anchorPositionTypemiddle' | '左' | '正确' | '顶部' | '底部

关于typescript type is not assignable to type `symbol` 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61290227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com