From 1ea269f42c2ef10e95fcf4751e71fba8bb5fe80d Mon Sep 17 00:00:00 2001
From: D N <4661784+retyui@users.noreply.github.com>
Date: Mon, 11 Mar 2024 06:00:36 -0700
Subject: [PATCH] fix: [TypeScript] Allow to pass empty string as style
(#43404)
Summary:
```tsx
import {Text} from 'react-native'
interface Props {
foregroundColor?: string | undefined | null
}
function Test({foregroundColor}: Props){
return
// ^^^ Error: Type "" is not assignable to type TextStyle | Falsy | RegisteredStyle | RecursiveArray;
```
Reviewed By: rshest
Differential Revision: D54744517
Pulled By: javache
fbshipit-source-id: c5b934616cc0501c2b6a7907e6be522187a2cc20
---
.../Libraries/StyleSheet/StyleSheet.d.ts | 2 +-
.../react-native/types/__typetests__/index.tsx | 14 +++++++++++---
2 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/packages/react-native/Libraries/StyleSheet/StyleSheet.d.ts b/packages/react-native/Libraries/StyleSheet/StyleSheet.d.ts
index 6d9fda4dac2..17ab55d6bc9 100644
--- a/packages/react-native/Libraries/StyleSheet/StyleSheet.d.ts
+++ b/packages/react-native/Libraries/StyleSheet/StyleSheet.d.ts
@@ -14,7 +14,7 @@ export interface StyleSheetProperties {
flatten(style: T): T;
}
-type Falsy = undefined | null | false;
+type Falsy = undefined | null | false | '';
interface RecursiveArray
extends Array | RecursiveArray> {}
/** Keep a brand of 'T' so that calls to `StyleSheet.flatten` can take `RegisteredStyle` and return `T`. */
diff --git a/packages/react-native/types/__typetests__/index.tsx b/packages/react-native/types/__typetests__/index.tsx
index 33a3a2bba74..80a615b036c 100644
--- a/packages/react-native/types/__typetests__/index.tsx
+++ b/packages/react-native/types/__typetests__/index.tsx
@@ -411,7 +411,9 @@ class CustomView extends React.Component {
}
}
-class Welcome extends React.Component & {color: string}> {
+class Welcome extends React.Component<
+ ElementProps & {color: string; bgColor?: null | undefined | string}
+> {
rootViewRef = React.useRef(null);
customViewRef = React.useRef(null);
@@ -436,12 +438,18 @@ class Welcome extends React.Component & {color: string}> {
}
render() {
- const {color, ...props} = this.props;
+ const {color, bgColor, ...props} = this.props;
return (
+ style={[
+ [styles.container],
+ undefined,
+ null,
+ false,
+ bgColor && {backgroundColor: bgColor},
+ ]}>
Welcome to React Native
To get started, edit index.ios.js