Andrew
Andrew

Reputation: 795

Interface restrict key value to be exact value of a const Typescript

I like to extend my question and ask for a help with interface example:

const FIRST = "FIRST"
const SECOND = "SECOND"

interface TSomeInterface {
    element: Element
    order?: typeof FIRST | typeof SECOND // not working, value could be anything
}

How do I restrict the optional order key to one of the const above?

To be clear, I want:

{ element: someElement, order: FIRST } // pass
{ element: someElement, order: SECOND } // pass
{ element: someElement, order: "test" } // fail
{ element: someElement, order: "" } // fail
{ element: someElement, order: 0 } // fail

Upvotes: 3

Views: 171

Answers (1)

user13258211
user13258211

Reputation:

Your snippet is already working.

Check your typescript configuration and as @Yoshi said, your typescript version.

Upvotes: 2

Related Questions