zhulien
zhulien

Reputation: 5695

Use object values as interface keys

I have an object containing property names in the form:

const INPUT_NAMES = {
    FIRST_NAME: 'first-name',
    LAST_NAME: 'last-name'
};

I want to generate an interface based on those property name values:

interface IPropertyFormat {
    [INPUT_NAMES.FIRST_NAME]: string;
    [INPUT_NAMES.LAST_NAME]: string;
}

That doesn't work because I am not using a literal type, nor a symbol and I get this: A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.

Using a simple constant like:

const KEY = "FIRST_NAME";

interface IPropertyFormat {
    [KEY]: string;
}

works but is not an elegant solution in my case and would like to avoid it if possible. I've checked out this other kinda related topic but it doesn't provide a solution for my problem.

Anyone has any idea how to work around this problem?

EDIT:

Also, found a "some-what" solution at this post once again using const assertions.

Upvotes: 1

Views: 844

Answers (1)

Ilario Pierbattista
Ilario Pierbattista

Reputation: 3265

To have the correct litteral types in INPUT_NAMES you should use as const

Link to playground

const INPUT_NAMES = {
    FIRST_NAME: 'first-name' as const,
    LAST_NAME: 'last-name' as const
};

type Result =  {
    [k in typeof INPUT_NAMES[keyof typeof INPUT_NAMES]]: string
}

type Expected = {
    'first-name': string,
    'last-name': string
}

declare const x: Expected
declare const y: Result
// In case Result and Expected are different types, there will be an error
const a: Expected = y
const b: Result = x

If having an interface is very important:

interface myInterface extends Result {}

Upvotes: 1

Related Questions