diff --git a/.flowconfig b/.flowconfig index 6c657dce6f54..cbfc6a77ee6a 100644 --- a/.flowconfig +++ b/.flowconfig @@ -45,6 +45,7 @@ packages/react-native/flow/ [options] enums=true experimental.pattern_matching=true +experimental.allow_variance_keywords=true casting_syntax=both component_syntax=true diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index cbf5db617106..41b8edabf542 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -72,14 +72,14 @@ export type ArrayBufferTypeAnnotation = Readonly<{ type: 'ArrayBufferTypeAnnotation', }>; -export type ObjectTypeAnnotation<+T> = Readonly<{ +export type ObjectTypeAnnotation = Readonly<{ type: 'ObjectTypeAnnotation', properties: ReadonlyArray>, // metadata for objects that generated from interfaces baseTypes?: ReadonlyArray, }>; -export type UnionTypeAnnotation<+T> = Readonly<{ +export type UnionTypeAnnotation = Readonly<{ type: 'UnionTypeAnnotation', types: ReadonlyArray, }>; @@ -93,13 +93,13 @@ export type EventEmitterTypeAnnotation = Readonly<{ typeAnnotation: NativeModuleEventEmitterTypeAnnotation | $FlowFixMe, }>; -type FunctionTypeAnnotation<+P, +R> = Readonly<{ +type FunctionTypeAnnotation = Readonly<{ type: 'FunctionTypeAnnotation', params: ReadonlyArray>, returnTypeAnnotation: R, }>; -export type NamedShape<+T> = Readonly<{ +export type NamedShape = Readonly<{ name: string, optional: boolean, typeAnnotation: T, @@ -195,7 +195,7 @@ export type ComponentCommandArrayTypeAnnotation = ArrayTypeAnnotation< | MixedTypeAnnotation, >; -export type ArrayTypeAnnotation<+T> = Readonly<{ +export type ArrayTypeAnnotation = Readonly<{ type: 'ArrayTypeAnnotation', elementType: T, }>; @@ -269,11 +269,11 @@ export type ReservedTypeAnnotation = Readonly<{ /** * NativeModule Types */ -export type Nullable<+T extends NativeModuleTypeAnnotation> = +export type Nullable = | NullableTypeAnnotation | T; -export type NullableTypeAnnotation<+T extends NativeModuleTypeAnnotation> = +export type NullableTypeAnnotation = Readonly<{ type: 'NullableTypeAnnotation', typeAnnotation: T, @@ -321,7 +321,7 @@ export type NativeModuleObjectTypeAnnotation = ObjectTypeAnnotation< >; export type NativeModuleArrayTypeAnnotation< - +T extends Nullable, + out T extends Nullable, > = ArrayTypeAnnotation< | T /** diff --git a/packages/react-native-codegen/src/parsers/flow/components/componentsUtils.js b/packages/react-native-codegen/src/parsers/flow/components/componentsUtils.js index c0cdfab9a7ad..b15150f9f573 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/componentsUtils.js +++ b/packages/react-native-codegen/src/parsers/flow/components/componentsUtils.js @@ -17,7 +17,7 @@ const {verifyPropNotAlreadyDefined} = require('../../parsers-commons'); const {getValueFromTypes} = require('../utils.js'); // $FlowFixMe[unsupported-variance-annotation] -function getTypeAnnotationForArray<+T>( +function getTypeAnnotationForArray( name: string, typeAnnotation: $FlowFixMe, defaultValue: $FlowFixMe | null, @@ -219,7 +219,7 @@ function flattenProperties( } // $FlowFixMe[unsupported-variance-annotation] -function getTypeAnnotation<+T>( +function getTypeAnnotation( name: string, annotation: $FlowFixMe | ASTNode, defaultValue: $FlowFixMe | null, diff --git a/packages/react-native-codegen/src/parsers/parsers-commons.js b/packages/react-native-codegen/src/parsers/parsers-commons.js index f483a3fb0faf..330e52fe8c76 100644 --- a/packages/react-native-codegen/src/parsers/parsers-commons.js +++ b/packages/react-native-codegen/src/parsers/parsers-commons.js @@ -107,7 +107,7 @@ function wrapModuleSchema( } // $FlowFixMe[unsupported-variance-annotation] -function unwrapNullable<+T extends NativeModuleTypeAnnotation>( +function unwrapNullable( x: Nullable, ): [T, boolean] { if (x.type === 'NullableTypeAnnotation') { @@ -118,7 +118,7 @@ function unwrapNullable<+T extends NativeModuleTypeAnnotation>( } // $FlowFixMe[unsupported-variance-annotation] -function wrapNullable<+T extends NativeModuleTypeAnnotation>( +function wrapNullable( nullable: boolean, typeAnnotation: T, ): Nullable { diff --git a/packages/react-native/Libraries/Animated/createAnimatedComponent.js b/packages/react-native/Libraries/Animated/createAnimatedComponent.js index ba58374c2087..435aea045ce6 100644 --- a/packages/react-native/Libraries/Animated/createAnimatedComponent.js +++ b/packages/react-native/Libraries/Animated/createAnimatedComponent.js @@ -31,7 +31,7 @@ type Nullable = void | null; type Primitive = string | number | boolean | symbol | void; type Builtin = (...ReadonlyArray) => unknown | Date | Error | RegExp; -export type WithAnimatedValue<+T> = T extends Builtin | Nullable +export type WithAnimatedValue = T extends Builtin | Nullable ? T : T extends Primitive ? @@ -91,7 +91,7 @@ export type AnimatedBaseProps = LooseOmit< export type AnimatedComponentType< Props extends {...}, - +Instance = unknown, + out Instance = unknown, > = component(ref?: React.RefSetter, ...AnimatedProps); export default function createAnimatedComponent< diff --git a/packages/react-native/Libraries/StyleSheet/StyleSheet.js b/packages/react-native/Libraries/StyleSheet/StyleSheet.js index 5752cf1e7368..ec480ba115b0 100644 --- a/packages/react-native/Libraries/StyleSheet/StyleSheet.js +++ b/packages/react-native/Libraries/StyleSheet/StyleSheet.js @@ -118,7 +118,7 @@ export type DangerouslyImpreciseStyleProp = * This will correctly give you the type 'absolute' | 'relative' */ export type TypeForStyleKey< - +key extends keyof ____DangerouslyImpreciseStyle_Internal, + out key extends keyof ____DangerouslyImpreciseStyle_Internal, > = ____DangerouslyImpreciseStyle_Internal[key]; /** diff --git a/packages/react-native/Libraries/StyleSheet/StyleSheet.js.flow b/packages/react-native/Libraries/StyleSheet/StyleSheet.js.flow index ba8532ba3345..101d782aa278 100644 --- a/packages/react-native/Libraries/StyleSheet/StyleSheet.js.flow +++ b/packages/react-native/Libraries/StyleSheet/StyleSheet.js.flow @@ -119,7 +119,7 @@ export type DangerouslyImpreciseStyleProp = * This will correctly give you the type 'absolute' | 'relative' */ export type TypeForStyleKey< - +key extends keyof ____DangerouslyImpreciseStyle_Internal, + out key extends keyof ____DangerouslyImpreciseStyle_Internal, > = ____DangerouslyImpreciseStyle_Internal[key]; /** diff --git a/packages/react-native/Libraries/StyleSheet/StyleSheetExports.js b/packages/react-native/Libraries/StyleSheet/StyleSheetExports.js index 152cd00dc92d..65306969c766 100644 --- a/packages/react-native/Libraries/StyleSheet/StyleSheetExports.js +++ b/packages/react-native/Libraries/StyleSheet/StyleSheetExports.js @@ -179,7 +179,7 @@ export default { * An identity function for creating style sheets. */ // $FlowFixMe[unsupported-variance-annotation] - create<+S extends ____Styles_Internal>(obj: S): Readonly { + create(obj: S): Readonly { // TODO: This should return S as the return type. But first, // we need to codemod all the callsites that are typing this // return value as a number (even though it was opaque). diff --git a/packages/react-native/Libraries/StyleSheet/StyleSheetExports.js.flow b/packages/react-native/Libraries/StyleSheet/StyleSheetExports.js.flow index 869bcd0c440f..2aa4ee1d3ffb 100644 --- a/packages/react-native/Libraries/StyleSheet/StyleSheetExports.js.flow +++ b/packages/react-native/Libraries/StyleSheet/StyleSheetExports.js.flow @@ -95,6 +95,6 @@ declare export const setStyleAttributePreprocessor: ( * An identity function for creating style sheets. */ // $FlowFixMe[unsupported-variance-annotation] -declare export const create: <+S extends ____Styles_Internal>( +declare export const create: ( obj: S & ____Styles_Internal, ) => Readonly; diff --git a/packages/react-native/Libraries/StyleSheet/StyleSheetTypes.js b/packages/react-native/Libraries/StyleSheet/StyleSheetTypes.js index 1bf46c32b078..5b38c6dbc2d5 100644 --- a/packages/react-native/Libraries/StyleSheet/StyleSheetTypes.js +++ b/packages/react-native/Libraries/StyleSheet/StyleSheetTypes.js @@ -1064,7 +1064,7 @@ export type ____DangerouslyImpreciseStyle_Internal = Readonly<{ ... }>; -export type StyleProp<+T> = +export type StyleProp = | null | void | T @@ -1100,7 +1100,7 @@ export type ____Styles_Internal = { // ____FlattenStyleProp_Helper should be considered internal. type FlattenDepthLimiter = [void, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; type ____FlattenStyleProp_Helper< - +TStyleProp extends StyleProp, + out TStyleProp extends StyleProp, Depth extends Values = 9, > = Depth extends 0 ? empty @@ -1114,7 +1114,7 @@ type ____FlattenStyleProp_Helper< : TStyleProp; export type ____FlattenStyleProp_Internal< - +TStyleProp extends StyleProp, + out TStyleProp extends StyleProp, > = ____FlattenStyleProp_Helper extends empty // $FlowFixMe[unclear-type] ? any diff --git a/packages/react-native/Libraries/Types/CoreEventTypes.js b/packages/react-native/Libraries/Types/CoreEventTypes.js index 1e30bd6047c0..d2d0b34deee5 100644 --- a/packages/react-native/Libraries/Types/CoreEventTypes.js +++ b/packages/react-native/Libraries/Types/CoreEventTypes.js @@ -10,7 +10,7 @@ import type {HostInstance} from '../../src/private/types/HostInstance'; -export type NativeSyntheticEvent<+T> = Readonly<{ +export type NativeSyntheticEvent = Readonly<{ bubbles: ?boolean, cancelable: ?boolean, currentTarget: number | HostInstance, diff --git a/packages/react-native/flow/dom.js.flow b/packages/react-native/flow/dom.js.flow index de74fda8b26b..678d6b52e8a9 100644 --- a/packages/react-native/flow/dom.js.flow +++ b/packages/react-native/flow/dom.js.flow @@ -1379,7 +1379,7 @@ declare class Attr extends Node { localName: string; } -declare class HTMLCollection<+Elem extends HTMLElement> { +declare class HTMLCollection { @@iterator(): Iterator; length: number; item(nameOrIndex?: any, optionalIndex?: any): Elem | null; diff --git a/packages/react-native/src/private/components/virtualcollection/Virtual.js b/packages/react-native/src/private/components/virtualcollection/Virtual.js index 8840ac17da4d..b2556864f4a5 100644 --- a/packages/react-native/src/private/components/virtualcollection/Virtual.js +++ b/packages/react-native/src/private/components/virtualcollection/Virtual.js @@ -19,7 +19,7 @@ export interface Item {} * An interface for a collection of items, without requiring that each item be * eagerly (or lazily) allocated. */ -export interface VirtualCollection<+T extends Item> { +export interface VirtualCollection { /** * The number of items in the collection. This can either be a numeric scalar * or a getter function that is computed on access. However, it should remain @@ -40,7 +40,7 @@ export interface VirtualCollection<+T extends Item> { * use, this is not recommended for larger arrays because each element of an * array is eagerly allocated. */ -export class VirtualArray<+T extends Item> implements VirtualCollection { +export class VirtualArray implements VirtualCollection { +size: number; +at: (index: number) => T; diff --git a/packages/react-native/src/private/components/virtualcollection/VirtualCollectionView.js b/packages/react-native/src/private/components/virtualcollection/VirtualCollectionView.js index b1f0ddacc3ff..c8ad06b036a2 100644 --- a/packages/react-native/src/private/components/virtualcollection/VirtualCollectionView.js +++ b/packages/react-native/src/private/components/virtualcollection/VirtualCollectionView.js @@ -40,7 +40,7 @@ export type VirtualCollectionGenerator = Readonly<{ }>; export type VirtualCollectionViewComponent = - component<+TItem extends Item>( + component( children: (item: TItem, key: string) => React.Node, items: VirtualCollection, itemToKey?: (TItem) => string, @@ -87,7 +87,7 @@ export function createVirtualCollectionView( VirtualLayout: VirtualCollectionLayoutComponent, {initial, next}: VirtualCollectionGenerator, ): VirtualCollectionViewComponent { - component VirtualCollectionView<+TItem extends Item>( + component VirtualCollectionView( children: (item: TItem, key: string) => React.Node, items: VirtualCollection, itemToKey: TItem => string = defaultItemToKey, diff --git a/packages/react-native/src/private/components/virtualcollection/column/VirtualColumn.js b/packages/react-native/src/private/components/virtualcollection/column/VirtualColumn.js index e1674d525402..aed3aef0b006 100644 --- a/packages/react-native/src/private/components/virtualcollection/column/VirtualColumn.js +++ b/packages/react-native/src/private/components/virtualcollection/column/VirtualColumn.js @@ -34,7 +34,7 @@ const VirtualColumn = createVirtualCollectionView( // TODO: Figure out component generic resolution. // @see https://fb.workplace.com/groups/flow/posts/29355518614070041 // export default VirtualColumn as VirtualCollectionViewComponent; -export default VirtualColumn as component<+TItem extends Item>( +export default VirtualColumn as component( children: (item: TItem, key: string) => React.Node, items: VirtualCollection, itemToKey?: (TItem) => string, diff --git a/packages/react-native/src/private/components/virtualcollection/row/VirtualRow.js b/packages/react-native/src/private/components/virtualcollection/row/VirtualRow.js index 92b4bbd6ab53..9c7922b9dc18 100644 --- a/packages/react-native/src/private/components/virtualcollection/row/VirtualRow.js +++ b/packages/react-native/src/private/components/virtualcollection/row/VirtualRow.js @@ -34,7 +34,7 @@ const VirtualRow = createVirtualCollectionView( // TODO: Figure out component generic resolution. // @see https://fb.workplace.com/groups/flow/posts/29355518614070041 // export default VirtualRow as VirtualCollectionViewComponent; -export default VirtualRow as component<+TItem extends Item>( +export default VirtualRow as component( children: (item: TItem, key: string) => React.Node, items: VirtualCollection, itemToKey?: (TItem) => string, diff --git a/packages/react-native/src/private/webapis/dom/oldstylecollections/HTMLCollection.js.flow b/packages/react-native/src/private/webapis/dom/oldstylecollections/HTMLCollection.js.flow index b024dfc9c68a..45715ab40884 100644 --- a/packages/react-native/src/private/webapis/dom/oldstylecollections/HTMLCollection.js.flow +++ b/packages/react-native/src/private/webapis/dom/oldstylecollections/HTMLCollection.js.flow @@ -10,7 +10,7 @@ import type {ArrayLike} from '../../utils/ArrayLikeUtils'; -declare export default class HTMLCollection<+T> +declare export default class HTMLCollection // $FlowFixMe[incompatible-variance] implements Iterable, ArrayLike { diff --git a/packages/react-native/src/private/webapis/dom/oldstylecollections/NodeList.js.flow b/packages/react-native/src/private/webapis/dom/oldstylecollections/NodeList.js.flow index ee11d7060e90..91e4bd877ef4 100644 --- a/packages/react-native/src/private/webapis/dom/oldstylecollections/NodeList.js.flow +++ b/packages/react-native/src/private/webapis/dom/oldstylecollections/NodeList.js.flow @@ -10,8 +10,10 @@ import type {ArrayLike} from '../../utils/ArrayLikeUtils'; -// $FlowFixMe[incompatible-variance] -declare export default class NodeList<+T> implements Iterable, ArrayLike { +declare export default class NodeList + // $FlowFixMe[incompatible-variance] + implements Iterable, ArrayLike +{ // This property should've been read-only as well, but Flow doesn't handle // read-only indexers correctly (thinks reads are writes and fails). // $FlowFixMe[incompatible-variance]