Reputation: 368
I can't find a way to lookup in runtime whether a certain property exists on a type. Like so (pseudocode)
import { MyType } from '@prisma/client';
MyType.hasProperty("foo") // true/false
MyType.allProperties() // ["foo", "bar", "stuff"]
Does anyone know a good solution? Thank you in advance !
Upvotes: 10
Views: 11613
Reputation: 187024
Types don't exist at runtime. But you can still inspect objects that exist at runtime. You can use the in
operator to check if an object contains a particular property, and you can use use Object.keys(someObj)
to get all keys on an object as an array.
const foo = { a: 123, b: 'bar' }
'a' in foo // true
Object.keys(foo) // ['a', 'b']
What you cannot do is get this info from a type or interface. It must be a real object that exists at runtime.
Upvotes: 7