如何创建一个包含不确定数量特定对象的对象。

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

how to type an object that contains a indeterminate amount of a specific object

问题

我需要输入一个NgRx操作的参数。此参数是一个包含不确定数量特定对象的对象。最后一个对象已在接口上进行了类型定义。

  1. export interface CustomDistribution {
  2. maxWindowsActive: number;
  3. resize: boolean;
  4. }

我尝试了这个,但它不起作用:

  1. export const updateCustomDistributions = createAction(
  2. '[Custom Distributions] update Custom Distributions',
  3. props<{ customDistributions: { [key: string]: CustomDistribution } }>(),
  4. );

这是我想要获取和类型化的预期对象的示例:

  1. distributions: {
  2. right: {
  3. maxWindowsActive: 1,
  4. resize: true
  5. },
  6. left: {
  7. maxWindowsActive: 2,
  8. resize: false
  9. }
  10. }
英文:

I need to type the parameter of an NgRx action. This parameter is an object that contains an indeterminate number of specific object. This last object is already type on a Interface.

  1. export interface CustomDistribution {
  2. maxWindowsActive: number,
  3. resize: boolean,
  4. }

I tried this but it doesnt work:

  1. export const updateCustomDistributions = createAction(
  2. &#39;[Custom Distributions] update Custom Distributions&#39;,
  3. props&lt;{ customDistributions: CustomDistribution{} }&gt;(),
  4. );

This is an example of the expected object I want to get and type:

  1. distributions: {
  2. right: {
  3. maxWindowsActive: 1;
  4. resize: true
  5. },
  6. left: {
  7. maxWindowsActive: 2;
  8. resize: false
  9. }
  10. }

答案1

得分: 2

你正在寻找的可能是这个:

  1. export interface CustomDistributionGroup {
  2. [key: string]: CustomDistribution;
  3. // 或者如果你想允许数字键,可以这样写
  4. // [key: number]: CustomDistribution;
  5. // [key: string | number]: CustomDistribution; // 同时允许字符串和数字
  6. // 感谢 @Brandon Taylor
  7. }

如果你定义了这样的接口,那么你可以使用它来创建一个拥有无限数量的 CustomDistribution 对象的对象。但它们需要一个键,这在你的示例中并没有提供 => 这是一个无效的对象。应该像这样:

  1. const someProps: CustomDistributionGroup = {
  2. first: { maxWindowsActive: 1, resize: true },
  3. second: { maxWindowsActive: 2, resize: false },
  4. third: { maxWindowsActive: 3, resize: false },
  5. // 等等
  6. };

你的示例应该不起作用,我很惊讶你的IDE没有抛出错误。

英文:

What you're looking for is most likely this:

  1. export interface CustomDistributionGroup {
  2. [key: string]: CustomDistribution
  3. // or like that, if you want to allow numeric keys
  4. // [key: number]: CustomDistribution
  5. // [key: string | number]: CustomDistribution // both string and number
  6. // thanks @Brandon Taylor
  7. }

If you define an interface like that, then you can use it to create an object with indefinite number of CustomDistribution objects. But they need a key, which you didn't provide in your example => an invalid object.
This is how it should look:

  1. const someProps: CustomDistributionGroup {
  2. first: { maxWindowsActive: 1; resize: true },
  3. second: { maxWindowsActive: 2; resize: false },
  4. third: { maxWindowsActive: 3; resize: false },
  5. // and so one
  6. }

Your example should not work and I'm surprised your IDE is not throwing errors.

huangapple
  • 本文由 发表于 2023年7月17日 19:58:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76704235.html
匿名

发表评论

匿名网友

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

确定