ChatGPT解决这个技术问题 Extra ChatGPT

An index signature parameter type cannot be a union type. Consider using a mapped object type instead

I'm trying to use the following pattern:

enum Option {
  ONE = 'one',
  TWO = 'two',
  THREE = 'three'
}

interface OptionRequirement {
  someBool: boolean;
  someString: string;
}

interface OptionRequirements {
  [key: Option]: OptionRequirement;
}

This seems very straightforward to me, however I get the following error:

An index signature parameter type cannot be a union type. Consider using a mapped object type instead.

What am I doing wrong?

Type of key can only be string, number or symbol. enum is not.

j
jcalz

You can use TS "in" operator and do this:

enum Options {
  ONE = 'one',
  TWO = 'two',
  THREE = 'three',
}
interface OptionRequirement {
  someBool: boolean;
  someString: string;
}
type OptionRequirements = {
  [key in Options]: OptionRequirement; // Note that "key in".
}

Erm, this doesn't compile? The TypeScript playground says: "A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type."
Change interface OptionRequirements to type OptionRequirements
this actually doesn't work for me: A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type
I have edited this answer to use a mapped type alias instead of an interface. The original answer does not compile under any version of TypeScript that I've seen, and certainly does not compile under the current version of TypeScript (4.0 as of Aug 2020). @NachoJusticiaRamos, if you could demonstrate that your original version actually works somewhere, in some version of TypeScript, then I'd be happy to revert the edit, along with a description of the environment you need to use to have it work. Cheers!
Can someone explain a little bit what's going on here? What is the underlying problem TS encounters in setting an enum variable as a key? And why does changing "interface" to "type" solve the issue?
u
unional

The simplest solution is to use Record

type OptionRequirements = Record<Options, OptionRequirement>

You can also implement it yourself as:

type OptionRequirements = {
  [key in Options]: OptionRequirement;
}

This construct is only available to type, but not interface.

The problem in your definition is saying the key of your interface should be of type Options, where Options is an enum, not a string, number, or symbol.

The key in Options means "for those specific keys that's in the union type Options".

type alias is more flexible and powerful than interface.

If your type does not need to be used in class, choose type over interface.


that was cool. Now what if I need this variable to be undefined, how can you initialize it later?
@KatLimRuiz: [key in Options]: OptionRequirement | undefined
Interesting... if you do something like keyof Options within the Record typing, it works fine, but if you do it via the "implement it yourself" route you get a syntax error, unable to do [keyof Options]: OptionRequirement. In my case, my Option is a type, not an enum.
Also, I wonder if there is a way to just enforce that the key be in the enum, but not have typescript complain that you have to exhaustively use all members of that enum...
Ah, of course... using Exclude you can constrain which members should be implemented in your Record!
A
AmerllicA

In my case:

export type PossibleKeysType =
  | 'userAgreement'
  | 'privacy'
  | 'people';

interface ProviderProps {
  children: React.ReactNode;
  items: {
    //   ↙ this colon was issue
    [key: PossibleKeysType]: Array<SectionItemsType>;
  };
}

I fixed it by using in operator instead of using :

~~~

interface ProviderProps {
  children: React.ReactNode;
  items: {
    //     ↙ use "in" operator
    [key in PossibleKeysType]: Array<SectionItemsType>;
  };
}

I
Igor Kurkov

I had some similar problem but my case was with another field property in interface so my solution as an example with optional field property with an enum for keys:

export enum ACTION_INSTANCE_KEY {
  cat = 'cat',
  dog = 'dog',
  cow = 'cow',
  book = 'book'
}

type ActionInstances = {
  [key in ACTION_INSTANCE_KEY]?: number; // cat id/dog id/cow id/ etc // <== optional
};

export interface EventAnalyticsAction extends ActionInstances { // <== need to be extended
  marker: EVENT_ANALYTIC_ACTION_TYPE; // <== if you wanna add another field to interface
}

S
Stefan

Instead of using an interface, use a mapped object type

enum Option {
  ONE = 'one',
  TWO = 'two',
  THREE = 'three'
}

type OptionKeys = keyof typeof Option;

interface OptionRequirement {
  someBool: boolean;
  someString: string;
}

type OptionRequirements = {                 // note type, not interface
  [key in OptionKeys]: OptionRequirement;   // key in
}

you don't need to add so many types, the solution of @Nacho Justicia Ramos works the ONLY thing that some people overlook is that the last type is a TYPE not an INTERFACE. Which you could create an interface from that type.
A
Alazzawi

In my case I needed the properties to be optional, so I created this generic type.

type PartialRecord<K extends string | number | symbol, T> = { [P in K]?: T; };

Then use it as such:

type MyTypes = 'TYPE_A' | 'TYPE_B' | 'TYPE_C';

interface IContent {
    name: string;
    age: number;
}

interface IExample {
    type: string;
    partials: PartialRecord<MyTypes, IContent>;
}

Example

const example : IExample = {
    type: 'some-type',
    partials: {
        TYPE_A : {
            name: 'name',
            age: 30
        },
        TYPE_C : {
            name: 'another name',
            age: 50
        }
    }
}

Q
Questioning

I had a similar issue. I was trying to use only specific keys when creating angular form validators.

export enum FormErrorEnum {
  unknown = 'unknown',
  customError = 'customError',
}

export type FormError = keyof typeof FormErrorEnum;

And the usage:

static customFunction(param: number, param2: string): ValidatorFn {
  return (control: AbstractControl): { [key: FormErrorEnum]?: any } => {
    return { customError: {param, param2} };
  };
}

This will allow for 1 - X number of keys to be used.


J
Joe

I faced a similar issue, I've found this simple solution that for me, worked like a charm: simply replace [] with ():

interface OptionRequirements {
  (key: Option): OptionRequirement;
}

In my case I needed to use a union type instead of an enum, but both work fine for me:

type DocumentId = 'invoiceId' | 'orderId';

export interface INote {
    id: number;
    organizationId: number;
    (key: DocumentId): number;
    //...
}