Skip to content
Permalink
Browse files

[react] deprecate $Supertype and $Subtype

Summary:
This bans the utility types `$Supertype` and `$Subtype` from Flow. These utilities are unsound and can usually be modeled using shapes or bounded generics.

This also changes the types of the proptypes from `lib/react.js` to `any`, since these relied on `$Subtype`.

Reviewed By: jbrown215

Differential Revision: D13244153

fbshipit-source-id: cd82d603b4ada59b792832ce0bcfbc14bc667914
  • Loading branch information...
dsainati1 authored and facebook-github-bot committed Dec 5, 2018
1 parent 5f25cfb commit d9350718d3e53cdbad1a5739ba95c5f82c0058a7
@@ -89,7 +89,7 @@ declare class React$Component<Props, State = void> {
static displayName?: ?string;
static childContextTypes: any;
static contextTypes: any;
static propTypes: $Subtype<{[_: $Keys<Props>]: any}>;
static propTypes: any;

// We don't add a type for `defaultProps` so that its type may be entirely
// inferred when we diff the type for `defaultProps` with `Props`. Otherwise
@@ -145,7 +145,7 @@ declare type React$AbstractComponentStatics<DefaultProps> = {
declare type React$StatelessFunctionalComponent<Props> = {
(props: Props, context: any): React$Node,
displayName?: ?string,
propTypes?: $Subtype<{[_: $Keys<Props>]: any}>,
propTypes?: any,
contextTypes?: any
};

@@ -217,7 +217,7 @@ declare module react {
declare export var version: string;

declare export function checkPropTypes<V>(
propTypes: $Subtype<{[_: $Keys<V>]: ReactPropsCheckType}>,
propTypes : any,
values: V,
location: string,
componentName: string,
@@ -22,6 +22,7 @@ type lint_kind =
| NonstrictImport
| UnclearType
| DeprecatedType
| DeprecatedUtility
| UnsafeGettersSetters
| InexactSpread
| UnnecessaryOptionalChain
@@ -46,6 +47,7 @@ let string_of_kind = function
| NonstrictImport -> "nonstrict-import"
| UnclearType -> "unclear-type"
| DeprecatedType -> "deprecated-type"
| DeprecatedUtility -> "deprecated-utility"
| UnsafeGettersSetters -> "unsafe-getters-setters"
| InexactSpread -> "inexact-spread"
| UnnecessaryOptionalChain -> "unnecessary-optional-chain"
@@ -73,6 +75,7 @@ let kinds_of_string = function
| "untyped-import" -> Some [UntypedImport]
| "unclear-type" -> Some [UnclearType]
| "deprecated-type" -> Some [DeprecatedType]
| "deprecated-utility" -> Some [DeprecatedUtility]
| "unsafe-getters-setters" -> Some [UnsafeGettersSetters]
| "inexact-spread" -> Some [InexactSpread]
| "unnecessary-optional-chain" -> Some [UnnecessaryOptionalChain]
@@ -22,6 +22,7 @@ type lint_kind =
| NonstrictImport
| UnclearType
| DeprecatedType
| DeprecatedUtility
| UnsafeGettersSetters
| InexactSpread
| UnnecessaryOptionalChain
@@ -2773,6 +2773,8 @@ let dump_flow_error =
spf "ENonstrictImport (%s)" (string_of_aloc loc)
| EUnclearType loc ->
spf "EUnclearType (%s)" (string_of_aloc loc)
| EDeprecatedUtility (loc, name) ->
spf "EDeprecatedUtility (%s, %s)" (string_of_aloc loc) name
| EDeprecatedType loc ->
spf "EDeprecatedType (%s)" (string_of_aloc loc)
| EUnsafeGettersSetters loc ->
@@ -152,6 +152,7 @@ type error_message =
| ENonstrictImport of ALoc.t
| EUnclearType of ALoc.t
| EDeprecatedType of ALoc.t
| EDeprecatedUtility of ALoc.t * string
| EUnsafeGettersSetters of ALoc.t
| EUnusedSuppression of ALoc.t
| ELintSetting of LintSettings.lint_parse_error
@@ -384,7 +385,8 @@ let util_use_op_of_msg nope util = function
| EUntypedImport (_, _)
| ENonstrictImport (_)
| EUnclearType (_)
| EDeprecatedType (_)
| EDeprecatedType _
| EDeprecatedUtility _
| EUnsafeGettersSetters (_)
| EUnusedSuppression (_)
| ELintSetting (_)
@@ -2072,15 +2074,20 @@ let rec error_of_msg ~trace_reasons ~source_file =
| EUnclearType loc ->
mk_error ~trace_infos ~kind:(LintError Lints.UnclearType) loc [
text "Unclear type. Using "; code "any"; text ", ";
code "Object"; text ", "; code "Function"; text ", ";
code "$Subtype<...>"; text ", or "; code "$Supertype<...>"; text " types is not safe!"
code "Object"; text ", or "; code "Function";
text " types is not safe!"
]

| EDeprecatedType loc ->
mk_error ~trace_infos ~kind:(LintError Lints.DeprecatedType) loc [
text "Deprecated type. Using "; code "*"; text " types is not recommended!"
]

| EDeprecatedUtility (loc, name) ->
mk_error ~trace_infos ~kind:(LintError Lints.DeprecatedUtility) loc [
text "Deprecated utility. Using "; code name; text " types is not recommended!"
]

| EUnsafeGettersSetters loc ->
mk_error ~trace_infos ~kind:(LintError Lints.UnsafeGettersSetters) loc
[text "Getters and setters can have side effects and are unsafe."]
@@ -2187,6 +2194,7 @@ let is_lint_error = function
| ENonstrictImport _
| EUnclearType _
| EDeprecatedType _
| EDeprecatedUtility _
| EUnsafeGettersSetters _
| ESketchyNullLint _
| ESketchyNumberLint _
@@ -302,9 +302,10 @@ let rec convert cx tparams_map = Ast.Type.(function
targs
)

(* These utilities are no longer supported *)
(* $Supertype<T> acts as any over supertypes of T *)
| "$Supertype" ->
add_unclear_type_error_if_not_lib_file cx loc;
FlowError.EDeprecatedUtility (loc, name) |> Flow_js.add_output cx;
check_type_arg_arity cx loc targs 1 (fun () ->
let ts, targs = convert_type_params () in
let t = List.hd ts in
@@ -313,7 +314,7 @@ let rec convert cx tparams_map = Ast.Type.(function

(* $Subtype<T> acts as any over subtypes of T *)
| "$Subtype" ->
add_unclear_type_error_if_not_lib_file cx loc;
FlowError.EDeprecatedUtility (loc, name) |> Flow_js.add_output cx;
check_type_arg_arity cx loc targs 1 (fun () ->
let ts, targs = convert_type_params () in
let t = List.hd ts in
@@ -61,7 +61,7 @@ References:
<BUILTINS>/react.js:219:41
v---
219| declare export function checkPropTypes<V>(
220| propTypes: $Subtype<{[_: $Keys<V>]: ReactPropsCheckType}>,
220| propTypes : any,
221| values: V,
222| location: string,
223| componentName: string,
@@ -82,7 +82,7 @@ References:
<BUILTINS>/react.js:219:41
v---
219| declare export function checkPropTypes<V>(
220| propTypes: $Subtype<{[_: $Keys<V>]: ReactPropsCheckType}>,
220| propTypes : any,
221| values: V,
222| location: string,
223| componentName: string,
@@ -91,15 +91,6 @@ References:
-------^ [1]


Error ------------------------------------------------------------------------------------------ checkPropTypes.js:10:43

Cannot call `checkPropTypes` with object literal bound to `values` because property `bar` is missing in object
literal [1].

10| checkPropTypes({ bar: PropTypes.string }, { foo: 'foo' }, 'value', 'TestComponent'); // error: property not found
^^^^^^^^^^^^^^ [1]


Error ------------------------------------------------------------------------------------------ checkPropTypes.js:12:85

Cannot call `checkPropTypes` with function bound to `getStack` because number [1] is incompatible with string [2] in the
@@ -139,4 +130,4 @@ References:



Found 9 errors
Found 8 errors
@@ -1,2 +1,5 @@
[options]
no_flowlib=false

[lints]
deprecated-utility=error
@@ -47,6 +47,14 @@ References:
^^^^ [1]


Error ----------------------------------------------------------------------------------------------------- test.js:17:6

Deprecated utility. Using `$Subtype` types is not recommended! (`deprecated-utility`)

17| x: $Subtype<{[key: $Keys<X>]: any}>; // object with larger key set than X's
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


Error ----------------------------------------------------------------------------------------------------- test.js:20:6

Property `qux` is missing in object type [1].
@@ -62,4 +70,4 @@ References:



Found 4 errors
Found 5 errors
@@ -23,8 +23,7 @@ References:

Error ---------------------------------------------------------------------------------------------------- test.js:12:10

Unclear type. Using `any`, `Object`, `Function`, `$Subtype<...>`, or `$Supertype<...>` types is not safe!
(`unclear-type`)
Unclear type. Using `any`, `Object`, or `Function` types is not safe! (`unclear-type`)

12| const x: any = {}; // Error: unclear-type
^^^
@@ -0,0 +1,10 @@
[ignore]

[include]

[libs]

[options]

[lints]
deprecated-utility=error
@@ -0,0 +1,18 @@
Error ------------------------------------------------------------------------------------------------------ test.js:3:9

Deprecated utility. Using `$Supertype` types is not recommended! (`deprecated-utility`)

3| let x : $Supertype<number> = 3;
^^^^^^^^^^^^^^^^^^


Error ------------------------------------------------------------------------------------------------------ test.js:4:9

Deprecated utility. Using `$Subtype` types is not recommended! (`deprecated-utility`)

4| let y : $Subtype<number> = 3;
^^^^^^^^^^^^^^^^



Found 2 errors
@@ -0,0 +1,4 @@
// @flow

let x : $Supertype<number> = 3;
let y : $Subtype<number> = 3;
Oops, something went wrong.

0 comments on commit d935071

Please sign in to comment.
You can’t perform that action at this time.