Skip to content

Commit

Permalink
Flow: use implicit exact object types
Browse files Browse the repository at this point in the history
Flow standard is now to use `{ }` for strict objects and `{ key: value, ... }` for open objects, see: facebook/relay@6fa0b0d and facebook/react-native@00cfb0f

This change should be without any problems as longs as users of our NPM libraries are using Flow with `exact_by_default=true` (which is now pretty standard I'd say).

Here is how object types behave before this change:

```
type A = { x: number }; // already "exact" type but disallowed because it would be confusing to mix different syntaxes
type B = { x: number, ... }; // this is inexact
type C = {| x: number |}; // this is explicitly exact and the only allowed way how to describe type "exactness"
```

Here is how object types behave _after_ this change:

```
type A = { x: number }; // this is the only allowed syntax for "exact" type
type B = { x: number, ... }; // this is still inexact
type C = {| x: number |}; // this is also exact but no longer allowed (so it's not confusing)
```

Some related (non-blocking) issues:

- gajus/eslint-plugin-flowtype#467
- facebook/flow#8612

adeira-source-id: 5f0c905ae627f670804581f78c1b570f3f71a1e6
  • Loading branch information
mrtnzlml authored and adeira-github-bot committed Apr 8, 2021
1 parent 9b7332c commit 42f6743
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 17 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Unreleased

**Breaking changes ahead!**

Note for all Flow users: all projects in [`adeira/universe`](https://github.com/adeira/universe) now use implicit exact Flow types (`{}` for strict objects and `{ ... }` for open objects, syntax `{||}` is deprecated). We do not expect any issues as long as you are using `exact_by_default=true` Flow option.

Because of this migration of [`adeira/universe`](https://github.com/adeira/universe) to implicit exact objects (step 4 in this article: https://medium.com/flow-type/on-the-roadmap-exact-objects-by-default-16b72933c5cf) we are changing the following rules:

```text
flowtype/require-exact-type OFF -> ERROR with "never"
flowtype/require-inexact-type ERROR -> OFF
flowtype/require-readonly-react-props ERROR -> ERROR with "useImplicitExactTypes:true"
```

In case you want to prolong the support for explicit exact objects you can simply reverse these rules and you should be good to go.

# 5.3.0

- Rule [`react/no-unstable-nested-components`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-unstable-nested-components.md) enabled (warnings or errors in strict mode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ react/no-multi-comp

import { PureComponent, Component, useState, type Element } from 'react';

type LetterProps = {|
type LetterProps = {
+letter: string,
+onClick: () => void,
|};
};

const A = 65; // ASCII character code

Expand All @@ -23,12 +23,12 @@ class Letter extends PureComponent<LetterProps> {
}
}

type AplhabetProps = {||};
type AplhabetProps = {};

type AplhabetState = {|
type AplhabetState = {
justClicked: null | string,
letters: $ReadOnlyArray<string>,
|};
};

export class Alphabet1 extends Component<AplhabetProps, AplhabetState> {
constructor(props: AplhabetProps) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { createContext, Component, type Node, type Context, type Element } from 'react';

type Props = {|
type Props = {
+accessToken?: string,
+children: Node,
|};
};

type State = {|
type State = {
accessToken: ?string,
|};
};

const MyContext: Context<State> = createContext({
accessToken: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const React = {
Component: class Component<S, P> {},
};

type Props = {||};
type State = {||};
type Props = {};
type State = {};

export default class MyComponent extends React.Component<void, State> {
props: Props;
Expand Down
19 changes: 16 additions & 3 deletions __tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,19 @@ Object {
"flowtype/object-type-curly-spacing": "off",
"flowtype/object-type-delimiter": "off",
"flowtype/require-compound-type-alias": 0,
"flowtype/require-exact-type": 0,
"flowtype/require-exact-type": Array [
1,
"never",
],
"flowtype/require-indexer-name": 0,
"flowtype/require-inexact-type": 2,
"flowtype/require-inexact-type": 0,
"flowtype/require-parameter-type": 0,
"flowtype/require-readonly-react-props": 2,
"flowtype/require-readonly-react-props": Array [
2,
Object {
"useImplicitExactTypes": true,
},
],
"flowtype/require-return-type": 0,
"flowtype/require-types-at-top": 0,
"flowtype/require-valid-file-annotation": Array [
Expand Down Expand Up @@ -1032,6 +1040,11 @@ Snapshot Diff:
- Value for stable rules
+ Value for STRICT rules
@@ --- --- @@
"flowtype/require-exact-type": Array [
- 1,
+ 2,
"never",
@@ --- --- @@
"flowtype/use-flow-type": 1,
- "flowtype/use-read-only-spread": 1,
Expand Down
11 changes: 8 additions & 3 deletions ourRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,16 @@ module.exports = ({
},
],
'flowtype/require-compound-type-alias': OFF,
'flowtype/require-exact-type': OFF,
'flowtype/require-exact-type': [NEXT_VERSION_ERROR, 'never'], // we are using `exact_by_default=true`
'flowtype/require-indexer-name': OFF,
'flowtype/require-inexact-type': ERROR,
'flowtype/require-inexact-type': OFF,
'flowtype/require-parameter-type': OFF,
'flowtype/require-readonly-react-props': ERROR,
'flowtype/require-readonly-react-props': [
ERROR,
{
useImplicitExactTypes: true,
},
],
'flowtype/require-return-type': OFF,
'flowtype/require-types-at-top': OFF,
'flowtype/require-valid-file-annotation': [ERROR, 'always'],
Expand Down

0 comments on commit 42f6743

Please sign in to comment.