Home / Function/ tryUnionTypes() — react Function Reference

tryUnionTypes() — react Function Reference

Architecture documentation for the tryUnionTypes() function in InferTypes.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  e60369e7_778a_215c_2939_89cf588096ef["tryUnionTypes()"]
  35147ed6_ce97_e85f_570c_faf2d25f42f4["InferTypes.ts"]
  e60369e7_778a_215c_2939_89cf588096ef -->|defined in| 35147ed6_ce97_e85f_570c_faf2d25f42f4
  6938eece_f870_22b1_d136_01eb0a26f7b9["bindVariableTo()"]
  6938eece_f870_22b1_d136_01eb0a26f7b9 -->|calls| e60369e7_778a_215c_2939_89cf588096ef
  style e60369e7_778a_215c_2939_89cf588096ef fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts lines 846–880

function tryUnionTypes(ty1: Type, ty2: Type): Type | null {
  let readonlyType: Type;
  let otherType: Type;
  if (ty1.kind === 'Object' && ty1.shapeId === BuiltInMixedReadonlyId) {
    readonlyType = ty1;
    otherType = ty2;
  } else if (ty2.kind === 'Object' && ty2.shapeId === BuiltInMixedReadonlyId) {
    readonlyType = ty2;
    otherType = ty1;
  } else {
    return null;
  }
  if (otherType.kind === 'Primitive') {
    /**
     * Union(Primitive | MixedReadonly) = MixedReadonly
     *
     * For example, `data ?? null` could return `data`, the fact that RHS
     * is a primitive doesn't guarantee the result is a primitive.
     */
    return readonlyType;
  } else if (
    otherType.kind === 'Object' &&
    otherType.shapeId === BuiltInArrayId
  ) {
    /**
     * Union(Array | MixedReadonly) = Array
     *
     * In practice this pattern means the result is always an array. Given
     * that this behavior requires opting-in to the mixedreadonly type
     * (via moduleTypeProvider) this seems like a reasonable heuristic.
     */
    return otherType;
  }
  return null;
}

Subdomains

Called By

Frequently Asked Questions

What does tryUnionTypes() do?
tryUnionTypes() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts.
Where is tryUnionTypes() defined?
tryUnionTypes() is defined in compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts at line 846.
What calls tryUnionTypes()?
tryUnionTypes() is called by 1 function(s): bindVariableTo.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free