Need to check if interface has specific property. Tried/googled, but could not find appropriate solution. For example;
interface Props {
id: string;
name: string;
age: number;
}
What is best way to check if Props has "age" property?
If what you're looking for is checking if a key exists at compile time in a type, you can use something like this:
type ContainsKey<T, K extends string | number> = T extends { [key in K]: any }
? true
: false
type contains1 = ContainsKey<Props, 'age'> // type contains1 = true
type contains2 = ContainsKey<Props, 'foo'> // type contains2 = false
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