0
votes
const EMPLOYEE = {
  KEY: 'key',
  NAME: 'name',
}

export interface Employee {
  [EMPLOYEE.KEY]: string;
  [EMPLOYEE.NAME]: string;
}

And I have an error: A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.

Can I use above code? Thank you!

1

1 Answers

0
votes

You should be able to accomplish this using an enum instead of a const:

enum EMPLOYEE {
  KEY = 'key',
  NAME = 'name',
}

export interface Employee {
  [EMPLOYEE.KEY]: string;
  [EMPLOYEE.NAME]: string;
}

Now you can do:

const employee: Employee = { key: 'some key', name: 'some name' };