‘X’ 属性的类型不兼容(在使用字符串联合时)。

huangapple go评论84阅读模式
英文:

Types of property 'X' are incompatible (when using string union)

问题

I have translated the code you provided into Chinese:

我有两个组件定义如下

const GridCell = <T extends keyof FormValue>(
  props: GridCellProps<T>,
) => {
....
}

const GridRow = <T extends keyof FormValue>(props: GridRowProps<T>) => {
...
  <GridCell
    type={props.type} // &lt;-- error here
    fractionDigits={props.fractionDigits}
    ...
    />;
...
}

我的类型定义如下


export type GridRowFieldType<T extends keyof FormValue> = {
  isDetail: boolean;
  highlighted: boolean;
  label: string;
  fieldName: T;
} & (
  | { type: 'date' | 'duration'; fractionDigits?: never }
  | { type: 'number'; fractionDigits: number }
);

export type GridRowProps<T extends keyof FormValue> = {
  showDetails: boolean;
  values: FormValue[T];
} & GridRowFieldType<T>;


export type GridCellProps<T extends keyof FormValue> = {
  label: string;
  fieldName: T;
  index: number;
} & (
  | {
      type: 'date';
      value: DateFieldValueType | undefined;
      fractionDigits?: never;
    }
  | {
      type: 'duration';
      value: DurationFieldValueType | undefined;
      fractionDigits?: never;
    }
  | { type: 'number'; value: number | undefined; fractionDigits: number }
);

出于某种原因我在`GridCell`组件中收到以下错误

属性'type'的类型不兼容
  类型'"number" | "date" | "duration"'无法分配给类型'"number"'
  类型'"date"'无法分配给类型'"number"'ts(2322)

尽管类型始终解析为'"number" | "date" | "duration"'但我仍然收到该错误而且不知道原因以及如何解决它

**编辑**

`GridCellProps`如果我更改

type: 'date' | 'duration' | 'number';

此错误会消失但我需要根据'type'属性的值来定义'value'属性的类型

**编辑2**

这是TS Playground看起来有点不同但问题是相同的):

https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgGIHsoFsBqcA2ArigN4CwAUMsjMBPgCYCMAXMgBQDOYUoA5sgA+yQiAYRaICAwCUAbQC6AbkrVa9BgCY27EISwAjaEJFiJoafOWqadRgGYdB9OnwQ4IE6PGTLilVS2GgAsOty8IALC3uZSsv426owArGE8-F5mvvHWFAC+lJRgAJ4ADigA4rwMqHYMADwAKsgQAB6QYpzIANYQxegwaJi4BMQAfMgAvMjkgfhwRvhs4fwBanUAcnBYEGyNAXnIAGQcNsKz1JfIJeVsAOSQ7XdrV8gAbqMQAPzL6ZEvVxgUEQYGA6BAABFgHxgGBOD9kFI3tAAcgCoFzjYrjddsg7s5XO4QM8sZcPkRvmwCW4PKi1MCEKDwVCYXCEUiUaT0dRztcyri7npDNBnu9Puz9EYoEoaAymZDobDOGwhVK0ZQZAEivzkFVgAwAEroADuUxmiTqyt11VqGnqvX6gww2DwFLGCXyWooCHB3CCjCtepqdXtfQGQxdn3dCjNchsF0u80W91tjGQTDuABpSUkGFsdvdcxns4FqDj7o8wFmbHkS9QE9Qk-QU3VkJpq6X-XntgLc+265dy3jVSKB-SQWCFayrUwB7XKLlKD6QH6sHBSqmGFb2EHN6HHRGRm6rGbc5wAHRr0rsdgwGRTCYJqAQMCEKCeBvIJtLGjn79jrt81xGBz1zICAKHECcQA8liDYEDYIgACgQnZlFTheDQLlScWSVGtKDyGQgA
英文:

I have two components defined as following:

const GridCell = &lt;T extends keyof FormValue&gt;(
props: GridCellProps&lt;T&gt;,
) =&gt; {
....
}
const GridRow = &lt;T extends keyof FormValue&gt;(props: GridRowProps&lt;T&gt;) =&gt; {
...
&lt;GridCell
type={props.type} // &lt;-- error here
fractionDigits={props.fractionDigits}
...
/&gt;
...
}

My types are defined as following:

export type GridRowFieldType&lt;T extends keyof FormValue&gt; = {
isDetail: boolean;
highlighted: boolean;
label: string;
fieldName: T;
} &amp; (
| { type: &#39;date&#39; | &#39;duration&#39;; fractionDigits?: never }
| { type: &#39;number&#39;; fractionDigits: number }
);
export type GridRowProps&lt;T extends keyof FormValue&gt; = {
showDetails: boolean;
values: FormValue[T];
} &amp; GridRowFieldType&lt;T&gt;;
export type GridCellProps&lt;T extends keyof FormValue&gt; = {
label: string;
fieldName: T;
index: number;
} &amp; (
| {
type: &#39;date&#39;;
value: DateFieldValueType | undefined;
fractionDigits?: never;
}
| {
type: &#39;duration&#39;;
value: DurationFieldValueType | undefined;
fractionDigits?: never;
}
| { type: &#39;number&#39;; value: number | undefined; fractionDigits: number }
);

For some reason I get this error in the GridCell component:

Types of property &#39;type&#39; are incompatible.
Type &#39;&quot;number&quot; | &quot;date&quot; | &quot;duration&quot;&#39; is not assignable to type &#39;&quot;number&quot;&#39;.
Type &#39;&quot;date&quot;&#39; is not assignable to type &#39;&quot;number&quot;&#39;.ts(2322)

Even though type always resolves as &quot;number&quot; | &quot;date&quot; | &quot;duration&quot;, I still get that error, and I have no idea why, and on how to resolve it.

Edit:

In GridCellProps if I change :

&amp; (
| {
type: &#39;date&#39;;
value: DateFieldValueType | undefined;
fractionDigits?: never;
}
| {
type: &#39;duration&#39;;
value: DurationFieldValueType | undefined;
fractionDigits?: never;
}
| { type: &#39;number&#39;; value: number | undefined; fractionDigits: number }
);

To:

type: &#39;date&#39; | &#39;duration&#39; | &#39;number&#39;;

This error disappears, but I need to define the type of the value prop depending on the value of the type prop.

Edit 2:

Here is the TS playground (it looks a bit different, but the issue is the same):

https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgGIHsoFsBqcA2ArigN4CwAUMsjMBPgCYCMAXMgBQDOYUoA5sgA+yQiAYRaICAwCUAbQC6AbkrVa9BgCY27EISwAjaEJFiJoafOWqadRgGYdB9OnwQ4IE6PGTLilVS2GgAsOty8IALC3uZSsv426owArGE8-F5mvvHWFAC+lJRgAJ4ADigA4rwMqHYMADwAKsgQAB6QYpzIANYQxegwaJi4BMQAfMgAvMjkgfhwRvhs4fwBanUAcnBYEGyNAXnIAGQcNsKz1JfIJeVsAOSQ7XdrV8gAbqMQAPzL6ZEvVxgUEQYGA6BAABFgHxgGBOD9kFI3tAAcgCoFzjYrjddsg7s5XO4QM8sZcPkRvmwCW4PKi1MCEKDwVCYXCEUiUaT0dRztcyri7npDNBnu9Puz9EYoEoaAymZDobDOGwhVK0ZQZAEivzkFVgAwAEroADuUxmiTqyt11VqGnqvX6gww2DwFLGCXyWooCHB3CCjCtepqdXtfQGQxdn3dCjNchsF0u80W91tjGQTDuABpSUkGFsdvdcxns4FqDj7o8wFmbHkS9QE9Qk-QU3VkJpq6X-XntgLc+265dy3jVSKB-SQWCFayrUwB7XKLlKD6QH6sHBSqmGFb2EHN6HHRGRm6rGbc5wAHRr0rsdgwGRTCYJqAQMCEKCeBvIJtLGjn79jrt81xGBz1zICAKHECcQA8liDYEDYIgACgQnZlFTheDQLlScWSVGtKDyGQgA

答案1

得分: 1

I have solved this error, the solution was to use a switch statement to check the type of each field and return an object with the appropriate properties based on the type, this ensures that we only assign the correct properties to fields of the appropriate type, here is the updated TS playground:

我已经解决了这个错误,解决方案是使用 switch 语句来检查每个字段的类型,并根据类型返回一个具有相应属性的对象,这确保我们只将正确的属性分配给相应类型的字段,这里是更新后的 TS playground:

https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgGIHsoFsBqcA2ArigN4CwAUMsjMBPgCYCMAXMgBQDOYUoA5sgA+yQiAYRaICAwCUAbQC6AbkrVa9BgCY27EISwAjaEJFiJoafOWqadRgGYdB9OnwQ4IE6PGTLilVS2GgAsOty8IALC3uZSsv426owArGE8-F5mvvHWFAC+lJRgAJ4ADigA4rwMqHYMADwAKsgQAB6QYpzIANYQxegwaJi4BMQAfMgAvMjkgfhwRvhs4fwBanUAcnBYEGyNAXnIAGQcNsKz1JfIJeVsAOSQ7XdrV8gAbqMQAPzL6ZEvVxgUEQYGA6BAABFgHxgGBOD9kFI3tAAcgCoFzjYrjddsg7s5XO4QM8sZcPkRvmwCW4PKi1MCEKDwVCYXCEUiUaT0dRztcyri7npDNBnu9Puz9EYoEoaAymZDobDOGwhVK0ZQZAEivzkFVgAwAEroADuUxmiTqyt11VqGnqvX6gww2DwFLGCXyWooCHB3CCjCtepqdXtfQGQxdn3dCjNchsF0u80W91tjGQTDuABpSUkGFsdvdcxns4FqDj7o8wFmbHkS9QE9Qk-QU3VkJpq6X-XntgLc+265dy3jVSKB-SQWCFayrUwB7XKLlKAB6JctKBQTDISg+kB+rBwUqphhW9hBo+hx0RkZuqxm3OcAB0+9K7HYMBkUwmCagEDAhCgngNsgTZLDQD4gWOXb5riMAPrm0GQUOsE4pB5LEGwsFoRAkFAhOzKKnCGFwXKk4skqNaUHkMiFBQK7IJwriEPK26+mAyDPkenAAMqMfKOhniGDrhs617jLe0z3k+B6vu+n7moEnDGrCCAABYcMh-IfkBCBwJwKAPG0VYsKS1A-n+AHya8iYLM2YEQSZlzwT2RFOTskHYvyREoQ51BYURWHuWiqI6XpeLUkSdzGZ2pm-v+gE+cBNmgbB9nRY5mzOWBrnYQlSEPt5aW+Z8-mfIFeTBbp+kjlAkUOWZcWWVZiXJnZSWBesGjQS5GVublnlgQVTVihSJUUu1sp4VOSouSR+HTmVLzolRShAA

英文:

I have solved this error, the solution was to use a switch statement to check the type of each field and return an object with the appropriate properties based on the type, this ensures that we only assign the correct properties to fields of the appropriate type, here is the updated TS playground:

https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgGIHsoFsBqcA2ArigN4CwAUMsjMBPgCYCMAXMgBQDOYUoA5sgA+yQiAYRaICAwCUAbQC6AbkrVa9BgCY27EISwAjaEJFiJoafOWqadRgGYdB9OnwQ4IE6PGTLilVS2GgAsOty8IALC3uZSsv426owArGE8-F5mvvHWFAC+lJRgAJ4ADigA4rwMqHYMADwAKsgQAB6QYpzIANYQxegwaJi4BMQAfMgAvMjkgfhwRvhs4fwBanUAcnBYEGyNAXnIAGQcNsKz1JfIJeVsAOSQ7XdrV8gAbqMQAPzL6ZEvVxgUEQYGA6BAABFgHxgGBOD9kFI3tAAcgCoFzjYrjddsg7s5XO4QM8sZcPkRvmwCW4PKi1MCEKDwVCYXCEUiUaT0dRztcyri7npDNBnu9Puz9EYoEoaAymZDobDOGwhVK0ZQZAEivzkFVgAwAEroADuUxmiTqyt11VqGnqvX6gww2DwFLGCXyWooCHB3CCjCtepqdXtfQGQxdn3dCjNchsF0u80W91tjGQTDuABpSUkGFsdvdcxns4FqDj7o8wFmbHkS9QE9Qk-QU3VkJpq6X-XntgLc+265dy3jVSKB-SQWCFayrUwB7XKLlKAB6JctKBQTDISg+kB+rBwUqphhW9hBo+hx0RkZuqxm3OcAB0+9K7HYMBkUwmCagEDAhCgngNsgTZLDQD4gWOXb5riMAPrm0GQUOsE4pB5LEGwsFoRAkFAhOzKKnCGFwXKk4skqNaUHkMiFBQK7IJwriEPK26+mAyDPkenAAMqMfKOhniGDrhs617jLe0z3k+B6vu+n7moEnDGrCCAABYcMh-IfkBCBwJwKAPG0VYsKS1A-n+AHya8iYLM2YEQSZlzwT2RFOTskHYvyREoQ51BYURWHuWiqI6XpeLUkSdzGZ2pm-v+gE+cBNmgbB9nRY5mzOWBrnYQlSEPt5aW+Z8-mfIFeTBbp+kjlAkUOWZcWWVZiXJnZSWBesGjQS5GVublnlgQVTVihSJUUu1sp4VOSouSR+HTmVLzolRShAA

huangapple
  • 本文由 发表于 2023年4月16日 23:32:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定