Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of a double-pipe in an object in React

I've come across some samples for a React DND and within one of them was the following code:

export type Author = {|
  id: string,
  name: string,
  avatarUrl: string,
  url: string,
|}

There are several export types like this with the double pipe {|...|} in object brackets, and, despite my research, I can't find anything that explains what it does. I assume it's due to there being multiple Authors that are combined into the final object (re: the following code) and the double-pipes prevent some sort of conflict.

const princess: Author = {
  id: '4',
  name: 'Princess bubblegum',
  url: '',
  avatarUrl: '',
};

export const authors: Author[] = [
  jake, BMO, finn, princess,
];
like image 933
Chimera.Zen Avatar asked Jan 15 '18 16:01

Chimera.Zen


1 Answers

This is a flowtype exact object type annotation.

https://flow.org/en/docs/types/objects/#toc-exact-object-types

Sometimes it is useful to disable this behavior and only allow a specific set of properties. For this, Flow supports “exact” object types.

Basically, it will not allow any props outside of the defined ones and should complain if you add, say... age: 40 to an Author object.

like image 78
Dimitar Christoff Avatar answered Oct 13 '22 07:10

Dimitar Christoff