简体   繁体   English

打字稿:类型“字符串”不可分配给类型

[英]Typescript: Type 'string' is not assignable to type

I have an object我有一个对象

export const TOOLTIP_PLACEMENTS = Object.freeze({
  TOP: 'top',
  BOTTOM: 'bottom',
  LEFT: 'left',
  RIGHT: 'right',
});

And a component:还有一个组件:

type Props = {
  text?: string;
  children: React.ReactNode;
  placement?: keyof typeof TOOLTIP_PLACEMENTS;
};

const Tooltip = ({
  text,
  children,
  placement = TOOLTIP_PLACEMENTS.BOTTOM,
}: Props) => (
  <StyleTippy
    content={<TitleContainer>{text}</TitleContainer>}
    placement={placement}
    arrow={true}
  >
    {children}
  </StyleTippy>
);

However, Typescript is complaining that I am sending it a string literal但是,Typescript 抱怨我向它发送了一个字符串文字

src/components/Tooltip.tsx:41:3 - error TS2322: Type 'string' is not assignable to type '"TOP" | "BOTTOM" | "LEFT" | "RIGHT"'.

41   placement = TOOLTIP_PLACEMENTS.BOTTOM,
     ~~~~~~~~~


How would I fix this?我将如何解决这个问题?

Since they are all strings just change your props types so that placement is a string.由于它们都是字符串,因此只需更改您的道具类型,以便放置是一个字符串。 When you use TOOLTIP_PLACEMENTS.BOTTOM you are using the value of bottom which is a string so that is whats what the placement prop is expecting.当您使用TOOLTIP_PLACEMENTS.BOTTOM您使用的是bottom的值,它是一个字符串,因此这就是放置道具所期望的。

type Props = {
  text?: string;
  children: React.ReactNode;
  placement?: string;
};

when you do TOOLTIP_PLACEMENT.BOTTOM you get its value, which is bottom the lowercase string value.当你做TOOLTIP_PLACEMENT.BOTTOM你会得到它的值,它是小写字符串值的bottom But keyof typeof TOOLTIP_PLACEMENT gets you the keys which are the uppercase keys of the object: BOTTOM | TOP | RIGHT | LEFT但是keyof typeof TOOLTIP_PLACEMENT您提供了对象的大写键的键: BOTTOM | TOP | RIGHT | LEFT BOTTOM | TOP | RIGHT | LEFT BOTTOM | TOP | RIGHT | LEFT . BOTTOM | TOP | RIGHT | LEFT that are not strings but a union type.那不是字符串而是联合类型。 In order to get the value of a key you can define a type for placement like this: type Placement = typeof TOOLTIP_PLACEMENTS[keyof typeof TOOLTIP_PLACEMENTS];为了获得键的值,您可以像这样定义放置类型: type Placement = typeof TOOLTIP_PLACEMENTS[keyof typeof TOOLTIP_PLACEMENTS]; which returns the value of the selected key.它返回所选键的值。 However this depends on the property StyleTippy expects.然而,这取决于StyleTippy期望的属性。 I would assign it the type Placement as defined before.我会为它分配之前定义的类型Placement

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿:类型“字符串”不可分配给自定义类型 - Typescript: Type 'string' is not assignable to custom type TypeScript 错误:类型“字符串”不可分配给排版类型 - TypeScript error: Type 'string' is not assignable to type for Typography React TypeScript:类型“string []”不可分配给类型“never []” - React TypeScript: Type 'string[]' is not assignable to type 'never[]' Typescript React - 类型“字符串”不可分配给类型“外观” - Typescript React - Type 'string' is not assignable to type 'Appearance' 类型“字符串”不可分配给类型“T”。 使用 typescript - Type 'string' is not assignable to type 'T'. using typescript 输入'字符串| 日期'不可分配给类型'ReactNode'.. Typescript - Type 'string | Date' is not assignable to type 'ReactNode'.. Typescript TypeScript:“字符串| 未定义”不能分配给“字符串”类型 - TypeScript: 'string | undefined' is not assignable to type 'string' 字符串类型别名不可分配给带有 typescript 的 Recoil 中的字符串 - string type alias is not assignable to string in Recoil with typescript TypeScript(Nodejs 错误) 输入 'string | undefined' 不可分配给类型 'string'。 类型“undefined”不可分配给类型“string” - TypeScript(Nodejs error) Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string 节点打字稿:键入 &#39;string | string[]&#39; 不可分配给类型 &#39;string&#39; - Node typescript: type 'string | string[]' is not assignable to type 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM