user3590319
user3590319

Reputation: 1

How to check if an object has certain properties with flow?

I have a javascript function:

function foo(obj: Object): any { return 42; }

I want to make sure the input object has a property named 'name' on it, how can I do that with flow?

Upvotes: 0

Views: 1255

Answers (1)

JeffMo
JeffMo

Reputation: 213

Using {name: string} will assert that any object that comes in to the function must have a name property whose value is a string:

function foo(obj: {name: string}): any { return 42; }

Because Flow represents object types structurally, you can pass any object that has at least a name property to this annotation.

Upvotes: 5

Related Questions