Can I check if a string exists as an interface key
interface list {
one: string
two: string
}
const myNumber = "one"
How do I check if myNumber value is an interface key
The type of Typescript is not a value.
Therefore, operation of Javascript is impossible.
However, in the example, the type can be set so that myNumber is the type corresponding to the key.
interface list {
one: string
two: string
}
const myNumber: keyof list = "one"; // myNumber allow only "one" or "two";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With