1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @flow
    
  8.  */
    
  9. 
    
  10. import {checkFormFieldValueStringCoercion} from 'shared/CheckStringCoercion';
    
  11. 
    
  12. export opaque type ToStringValue =
    
  13.   | boolean
    
  14.   | number
    
  15.   | Object
    
  16.   | string
    
  17.   | null
    
  18.   | void;
    
  19. 
    
  20. // Flow does not allow string concatenation of most non-string types. To work
    
  21. // around this limitation, we use an opaque type that can only be obtained by
    
  22. // passing the value through getToStringValue first.
    
  23. export function toString(value: ToStringValue): string {
    
  24.   // The coercion safety check is performed in getToStringValue().
    
  25.   // eslint-disable-next-line react-internal/safe-string-coercion
    
  26.   return '' + (value: any);
    
  27. }
    
  28. 
    
  29. export function getToStringValue(value: mixed): ToStringValue {
    
  30.   switch (typeof value) {
    
  31.     case 'boolean':
    
  32.     case 'number':
    
  33.     case 'string':
    
  34.     case 'undefined':
    
  35.       return value;
    
  36.     case 'object':
    
  37.       if (__DEV__) {
    
  38.         checkFormFieldValueStringCoercion(value);
    
  39.       }
    
  40.       return value;
    
  41.     default:
    
  42.       // function, symbol are assigned as empty strings
    
  43.       return '';
    
  44.   }
    
  45. }