简体   繁体   English

字符串类型别名不可分配给带有 typescript 的 Recoil 中的字符串

[英]string type alias is not assignable to string in Recoil with typescript

export type OrderOption =
  | '-createdAt'
  | 'participationFee';

export const orderState = atom<OrderOption>({
  key: 'order',
  default: '-createdAt',
});

interface OrderListProps {
  options: { name: string; content: string }[];
  recoilState: RecoilState<string>;
}

const OrderList = ({ options, recoilState }: OrderListProps) => {
return some components }

and When I try rendering当我尝试渲染时

<OrderList options={ORDER_OPTIONS} recoilState={orderState} />

It causes ts(2322) error that says RecoilState<OrderOption> isn't assignable to RecoilState<string> .它会导致 ts(2322) 错误说RecoilState<OrderOption>不能分配给RecoilState<string>

If vice versa I understand the error, but OrderOption is still string overall so I don't understand why it's problematic.如果反之亦然,我理解错误,但OrderOption总体上仍然是字符串,所以我不明白为什么它有问题。 How can I solve this?我该如何解决这个问题?

RecoilState<'order' | '-createdAt'> RecoilState<'order' | '-createdAt'> is not the same as RecoilState<string> . RecoilState<'order' | '-createdAt'>RecoilState<string>


Define recoilState as RecoilState<'order' | '-createdAt'>recoilState定义为RecoilState<'order' | '-createdAt'> RecoilState<'order' | '-createdAt'>

(Same as RecoilState<OrderOption>) (与RecoilState<OrderOption>)

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

相关问题 打字稿:类型“字符串”不可分配给类型 - Typescript: Type 'string' is not assignable to type Typescript 字符串枚举类型 'string' 不可分配给类型 - Typescript String enum type 'string' is not assignable to type TypeScript:“字符串| 未定义”不能分配给“字符串”类型 - TypeScript: 'string | undefined' is not assignable to type 'string' 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: 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[]' 类型“string[]”不可分配给类型“never[]”。 Typescript 错误 - Type 'string[]' is not assignable to type 'never[]'. Typescript Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM