Skip to content

Codegen using generateOptionalProperties generates no optional for e.g. Float #55315

Description

@hannojg

Description

In this PR:

support was added in codegen for specifying generateOptionalProperties: true which should generate optional props as std::optional in the c++ binding.
I tested it and it works well for e.g. string.

However, when tested with Float it never generates the optional condition. The reason seems to be that for Float we pick a default value like 0.0.

E.g. for this prop:

export interface NativeProps extends ViewProps {
  animatedHeight?: Float;

and adding a comment here:
https://github.com/facebook/react-native/blob/ca520ee4c19dd921f9225c71a9ad872c4bbd7cb9/packages/react-native-codegen/src/generators/components/GeneratePropsCpp.js#L112-L120

We can see that the float comes with a default value:

{
  name: 'animatedHeight',
  optional: true,
  typeAnnotation: { type: 'FloatTypeAnnotation', default: 0 }

I feel like when specifying generateOptionalProperties: true this is unexpected behaviour.
If i were to specify:

export interface NativeProps extends ViewProps {
  animatedHeight?: WithDefault<Float, 0>;

i agree that the default would be correct.

This can actually be worked around by specifying:

export interface NativeProps extends ViewProps {
  animatedHeight?: WithDefault<Float, null>;

I wanted to open this issue to open a discussion whether we should:
a) Add a check in codegen if generateOptionalProperties: true and .default == defaultTypeForType (e.g. 0), so to check for the default value
b) if a property is optional and using generateOptionalProperties never put down a default value, if not explicitly specified (ie. this is a bug in the ts parser) (ie change the change to if (!prop.typeAnnotation.default))
c) Call this fine and do nothing?

Steps to reproduce

Create a simple typescript spec as above and run codegen.

React Native Version

0.83.1

Affected Platforms

Runtime - Android

Output of npx @react-native-community/cli info

System:
  OS: macOS 15.7.3
  CPU: (14) arm64 Apple M4 Pro
  Memory: 159.64 MB / 48.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 22.14.0
    path: /nix/store/04fc23dsflkxl4s9p6lkigia1hq3vjp2-nodejs-22.14.0/bin/node
  Yarn:
    version: 4.9.2
    path: /nix/store/04fc23dsflkxl4s9p6lkigia1hq3vjp2-nodejs-22.14.0/bin/yarn
  npm:
    version: 10.9.2
    path: /nix/store/04fc23dsflkxl4s9p6lkigia1hq3vjp2-nodejs-22.14.0/bin/npm
  Watchman:
    version: 2024.03.11.00
    path: /Users/hannogodecke/Documents/discord/.local/bin/watchman
Managers:
  CocoaPods:
    version: 1.16.2
    path: /Users/hannogodecke/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 25.2
      - iOS 26.2
      - macOS 26.2
      - tvOS 26.2
      - visionOS 26.2
      - watchOS 26.2
  Android SDK:
    API Levels:
      - "31"
      - "34"
      - "35"
      - "36"
    Build Tools:
      - 34.0.0
      - 35.0.0
      - 35.0.1
      - 36.0.0
    System Images:
      - android-35 | Google APIs ARM 64 v8a
      - android-35 | Google Play ARM 64 v8a
      - android-35 | Pre-Release 16 KB Page Size Google Play ARM 64 v8a
      - android-35 | Pre-Release 16 KB Page Size Google APIs ARM 64 v8a
      - android-35 | Google APIs ATD ARM 64
      - android-36 | Google Play ARM 64 v8a
    Android NDK: Not Found
IDEs:
  Android Studio: 2025.2 AI-252.25557.131.2521.14432022
  Xcode:
    version: 26.2/17C52
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.15
    path: /Users/hannogodecke/.jenv/shims/javac
  Ruby:
    version: 3.4.4
    path: /Users/hannogodecke/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react: Not Found
  react-native:
    installed: 0.83.1
    wanted: "*"
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: Not found
  newArchEnabled: Not found
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found

Stacktrace or Logs

No logs, this is more of a discussion item

MANDATORY Reproducer

https://github.com/hannojg/react-native/blob/main/packages/rn-tester/js/examples/Playground/RNTesterPlayground.js

Screenshots and Videos

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: AttentionIssues where the author has responded to feedback.Needs: ReproThis issue could be improved with a clear list of steps to reproduce the issue.StaleThere has been a lack of activity on this issue and it may be closed soon.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions