Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

javascript - 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,
];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...