Introduce BooleanLiteralTypeAnnotation (#54590)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/54590

Introduce `BooleanLiteralTypeAnnotation` in Flow & TypeScript to match the existing `StringLiteralTypeAnnotation` & `NumberLiteralTypeAnnotation` since Unions will be supporting Booleans along with String & Number

Changelog: [Internal]

Reviewed By: elicwhite

Differential Revision: D87384473

fbshipit-source-id: 6b8265914c7884ac99ae69e8992bc9dc6a9e52bc
This commit is contained in:
Arushi Kesarwani authored and meta-codesync[bot] committed 2025-11-20 18:00:53 -08:00
1 parent b46427a673
commit 2d22fed85c
22 files changed
+169

No files matched your search

+7
View File
@@ -46,6 +46,11 @@ export interface VoidTypeAnnotation {
readonly type: 'VoidTypeAnnotation';
}
export interface BooleanLiteralTypeAnnotation {
readonly type: 'BooleanLiteralTypeAnnotation';
readonly value: boolean;
}
export interface ObjectTypeAnnotation<T> {
readonly type: 'ObjectTypeAnnotation';
readonly properties: readonly NamedShape<T>[];
@@ -399,6 +404,7 @@ export type NativeModuleEventEmitterBaseTypeAnnotation =
| NativeModuleInt32TypeAnnotation
| NativeModuleNumberTypeAnnotation
| NumberLiteralTypeAnnotation
| BooleanLiteralTypeAnnotation
| NativeModuleStringTypeAnnotation
| StringLiteralTypeAnnotation
| StringLiteralUnionTypeAnnotation
@@ -416,6 +422,7 @@ export type NativeModuleBaseTypeAnnotation =
| StringLiteralUnionTypeAnnotation
| NativeModuleNumberTypeAnnotation
| NumberLiteralTypeAnnotation
| BooleanLiteralTypeAnnotation
| NativeModuleInt32TypeAnnotation
| NativeModuleDoubleTypeAnnotation
| NativeModuleFloatTypeAnnotation
+7
View File
@@ -56,6 +56,11 @@ export type StringLiteralTypeAnnotation = $ReadOnly<{
value: string,
}>;
export type BooleanLiteralTypeAnnotation = $ReadOnly<{
type: 'BooleanLiteralTypeAnnotation',
value: boolean,
}>;
export type StringLiteralUnionTypeAnnotation = $ReadOnly<{
type: 'StringLiteralUnionTypeAnnotation',
types: $ReadOnlyArray<StringLiteralTypeAnnotation>,
@@ -388,6 +393,7 @@ type NativeModuleEventEmitterBaseTypeAnnotation =
| Int32TypeAnnotation
| NativeModuleNumberTypeAnnotation
| NumberLiteralTypeAnnotation
| BooleanLiteralTypeAnnotation
| StringTypeAnnotation
| StringLiteralTypeAnnotation
| StringLiteralUnionTypeAnnotation
@@ -405,6 +411,7 @@ export type NativeModuleBaseTypeAnnotation =
| StringLiteralUnionTypeAnnotation
| NativeModuleNumberTypeAnnotation
| NumberLiteralTypeAnnotation
| BooleanLiteralTypeAnnotation
| Int32TypeAnnotation
| DoubleTypeAnnotation
| FloatTypeAnnotation
@@ -96,6 +96,8 @@ function serializeArg(
return wrap(val => `${val}.asString(rt)`);
case 'BooleanTypeAnnotation':
return wrap(val => `${val}.asBool()`);
case 'BooleanLiteralTypeAnnotation':
return wrap(val => `${val}.asBool()`);
case 'EnumDeclaration':
switch (realTypeAnnotation.memberType) {
case 'NumberTypeAnnotation':
@@ -265,6 +267,8 @@ function translatePrimitiveJSTypeToCpp(
return wrapOptional('int', isRequired);
case 'BooleanTypeAnnotation':
return wrapOptional('bool', isRequired);
case 'BooleanLiteralTypeAnnotation':
return wrapOptional('bool', isRequired);
case 'EnumDeclaration':
switch (realTypeAnnotation.memberType) {
case 'NumberTypeAnnotation':
@@ -142,6 +142,7 @@ function translateEventEmitterTypeToJavaType(
case 'Int32TypeAnnotation':
return 'double';
case 'BooleanTypeAnnotation':
case 'BooleanLiteralTypeAnnotation':
return 'boolean';
case 'GenericObjectTypeAnnotation':
case 'ObjectTypeAnnotation':
@@ -214,6 +215,8 @@ function translateFunctionParamToJavaType(
return wrapOptional('double', isRequired);
case 'BooleanTypeAnnotation':
return wrapOptional('boolean', isRequired);
case 'BooleanLiteralTypeAnnotation':
return wrapOptional('boolean', isRequired);
case 'EnumDeclaration':
switch (realTypeAnnotation.memberType) {
case 'NumberTypeAnnotation':
@@ -310,6 +313,8 @@ function translateFunctionReturnTypeToJavaType(
return wrapOptional('double', isRequired);
case 'BooleanTypeAnnotation':
return wrapOptional('boolean', isRequired);
case 'BooleanLiteralTypeAnnotation':
return wrapOptional('boolean', isRequired);
case 'EnumDeclaration':
switch (realTypeAnnotation.memberType) {
case 'NumberTypeAnnotation':
@@ -388,6 +393,8 @@ function getFalsyReturnStatementFromReturnType(
return nullable ? 'return null;' : 'return 0;';
case 'BooleanTypeAnnotation':
return nullable ? 'return null;' : 'return false;';
case 'BooleanLiteralTypeAnnotation':
return nullable ? 'return null;' : 'return false;';
case 'EnumDeclaration':
switch (realTypeAnnotation.memberType) {
case 'NumberTypeAnnotation':
@@ -171,6 +171,8 @@ function translateReturnTypeToKind(
return 'StringKind';
case 'BooleanTypeAnnotation':
return 'BooleanKind';
case 'BooleanLiteralTypeAnnotation':
return 'BooleanKind';
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
@@ -256,6 +258,8 @@ function translateParamTypeToJniType(
return 'Ljava/lang/String;';
case 'BooleanTypeAnnotation':
return !isRequired ? 'Ljava/lang/Boolean;' : 'Z';
case 'BooleanLiteralTypeAnnotation':
return !isRequired ? 'Ljava/lang/Boolean;' : 'Z';
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
@@ -338,6 +342,8 @@ function translateReturnTypeToJniType(
return 'Ljava/lang/String;';
case 'BooleanTypeAnnotation':
return nullable ? 'Ljava/lang/Boolean;' : 'Z';
case 'BooleanLiteralTypeAnnotation':
return nullable ? 'Ljava/lang/Boolean;' : 'Z';
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
@@ -11,6 +11,7 @@
'use strict';
import type {
BooleanLiteralTypeAnnotation,
BooleanTypeAnnotation,
DoubleTypeAnnotation,
FloatTypeAnnotation,
@@ -65,6 +66,7 @@ export type StructTypeAnnotation =
| StringLiteralUnionTypeAnnotation
| NativeModuleNumberTypeAnnotation
| NumberLiteralTypeAnnotation
| BooleanLiteralTypeAnnotation
| Int32TypeAnnotation
| DoubleTypeAnnotation
| FloatTypeAnnotation
@@ -110,6 +110,8 @@ function toObjCType(
return wrapCxxOptional('double', isRequired);
case 'BooleanTypeAnnotation':
return wrapCxxOptional('bool', isRequired);
case 'BooleanLiteralTypeAnnotation':
return wrapCxxOptional('bool', isRequired);
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
@@ -195,6 +197,8 @@ function toObjCValue(
return wrapPrimitive('double');
case 'BooleanTypeAnnotation':
return wrapPrimitive('BOOL');
case 'BooleanLiteralTypeAnnotation':
return wrapPrimitive('BOOL');
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
@@ -101,6 +101,8 @@ function toObjCType(
return wrapCxxOptional('double', isRequired);
case 'BooleanTypeAnnotation':
return wrapCxxOptional('bool', isRequired);
case 'BooleanLiteralTypeAnnotation':
return wrapCxxOptional('bool', isRequired);
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
@@ -185,6 +187,8 @@ function toObjCValue(
return RCTBridgingTo('Double');
case 'BooleanTypeAnnotation':
return RCTBridgingTo('Bool');
case 'BooleanLiteralTypeAnnotation':
return RCTBridgingTo('Bool');
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
@@ -28,6 +28,7 @@ function getEventEmitterTypeObjCType(
case 'NumberLiteralTypeAnnotation':
return 'NSNumber *_Nonnull';
case 'BooleanTypeAnnotation':
case 'BooleanLiteralTypeAnnotation':
return 'BOOL';
case 'GenericObjectTypeAnnotation':
case 'ObjectTypeAnnotation':
@@ -273,6 +273,8 @@ function getParamObjCType(
return notStruct(isRequired ? 'NSInteger' : 'NSNumber *');
case 'BooleanTypeAnnotation':
return notStruct(isRequired ? 'BOOL' : 'NSNumber *');
case 'BooleanLiteralTypeAnnotation':
return notStruct(isRequired ? 'BOOL' : 'NSNumber *');
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
@@ -356,6 +358,8 @@ function getReturnObjCType(
return wrapOptional('NSNumber *', isRequired);
case 'BooleanTypeAnnotation':
return wrapOptional('NSNumber *', isRequired);
case 'BooleanLiteralTypeAnnotation':
return wrapOptional('NSNumber *', isRequired);
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
@@ -428,6 +432,8 @@ function getReturnJSType(
return 'NumberKind';
case 'BooleanTypeAnnotation':
return 'BooleanKind';
case 'BooleanLiteralTypeAnnotation':
return 'BooleanKind';
case 'GenericObjectTypeAnnotation':
return 'ObjectKind';
case 'EnumDeclaration':
@@ -126,6 +126,7 @@ import * as TurboModuleRegistry from '../TurboModuleRegistry';
export interface Spec extends TurboModule {
+passBool?: (arg: boolean) => void;
+passNumber: (arg: number) => void;
+passBooleanLiteral: (arg: true) => void;
+passNumberLiteral: (arg: 4) => void;
+passString: (arg: string) => void;
+passStringish: (arg: Stringish) => void;
@@ -1252,6 +1252,26 @@ exports[`RN Codegen Flow Parser can generate fixture NATIVE_MODULE_WITH_BASIC_PA
]
}
},
{
'name': 'passBooleanLiteral',
'optional': false,
'typeAnnotation': {
'type': 'FunctionTypeAnnotation',
'returnTypeAnnotation': {
'type': 'VoidTypeAnnotation'
},
'params': [
{
'name': 'arg',
'optional': false,
'typeAnnotation': {
'type': 'BooleanLiteralTypeAnnotation',
'value': true
}
}
]
}
},
{
'name': 'passNumberLiteral',
'optional': false,
@@ -35,6 +35,7 @@ const {
} = require('../../parsers-commons');
const {
emitArrayType,
emitBooleanLiteral,
emitCommonTypes,
emitDictionary,
emitFunction,
@@ -249,6 +250,9 @@ function translateTypeAnnotation(
case 'NumberLiteralTypeAnnotation': {
return emitNumberLiteral(nullable, typeAnnotation.value);
}
case 'BooleanLiteralTypeAnnotation': {
return emitBooleanLiteral(nullable, typeAnnotation.value);
}
case 'StringLiteralTypeAnnotation': {
return wrapNullable(nullable, {
type: 'StringLiteralTypeAnnotation',
@@ -11,6 +11,7 @@
'use strict';
import type {
BooleanLiteralTypeAnnotation,
BooleanTypeAnnotation,
DoubleTypeAnnotation,
EventTypeAnnotation,
@@ -181,6 +182,16 @@ function emitNumberLiteral(
});
}
function emitBooleanLiteral(
nullable: boolean,
value: boolean,
): Nullable<BooleanLiteralTypeAnnotation> {
return wrapNullable(nullable, {
type: 'BooleanLiteralTypeAnnotation',
value,
});
}
function emitString(nullable: boolean): Nullable<StringTypeAnnotation> {
return wrapNullable(nullable, {
type: 'StringTypeAnnotation',
@@ -764,6 +775,7 @@ function emitUnionProp(
module.exports = {
emitArrayType,
emitBoolean,
emitBooleanLiteral,
emitBoolProp,
emitDouble,
emitDoubleProp,
@@ -113,6 +113,7 @@ import * as TurboModuleRegistry from '../TurboModuleRegistry';
export interface Spec extends TurboModule {
readonly passBool?: (arg: boolean) => void;
readonly passNumber: (arg: number) => void;
readonly passBooleanLiteral: (arg: true) => void;
readonly passNumberLiteral: (arg: 4) => void;
readonly passString: (arg: string) => void;
readonly passStringish: (arg: Stringish) => void;
@@ -1393,6 +1393,26 @@ exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_BA
]
}
},
{
'name': 'passBooleanLiteral',
'optional': false,
'typeAnnotation': {
'type': 'FunctionTypeAnnotation',
'returnTypeAnnotation': {
'type': 'VoidTypeAnnotation'
},
'params': [
{
'name': 'arg',
'optional': false,
'typeAnnotation': {
'type': 'BooleanLiteralTypeAnnotation',
'value': true
}
}
]
}
},
{
'name': 'passNumberLiteral',
'optional': false,
@@ -34,6 +34,7 @@ const {
const {parseObjectProperty} = require('../../parsers-commons');
const {
emitArrayType,
emitBooleanLiteral,
emitCommonTypes,
emitDictionary,
emitFunction,
@@ -409,6 +410,9 @@ function translateTypeAnnotation(
case 'NumericLiteral': {
return emitNumberLiteral(nullable, literal.value);
}
case 'BooleanLiteral': {
return emitBooleanLiteral(nullable, literal.value);
}
default: {
throw new UnsupportedTypeAnnotationParserError(
hasteModuleName,
@@ -173,6 +173,8 @@ function formatTypeAnnotation(annotation: CompleteTypeAnnotation): string {
return 'int';
case 'NumberLiteralTypeAnnotation':
return annotation.value.toString();
case 'BooleanLiteralTypeAnnotation':
return annotation.value.toString();
case 'ObjectTypeAnnotation':
return (
'{' +
@@ -116,6 +116,9 @@ export function compareTypeAnnotationForSorting(
case 'NumberLiteralTypeAnnotation':
invariant(typeB.type === 'NumberLiteralTypeAnnotation', EQUALITY_MSG);
return typeA.value - typeB.value;
case 'BooleanLiteralTypeAnnotation':
invariant(typeB.type === 'BooleanLiteralTypeAnnotation', EQUALITY_MSG);
return originalPositionA - originalPositionB;
case 'ObjectTypeAnnotation':
invariant(typeB.type === 'ObjectTypeAnnotation', EQUALITY_MSG);
return compareNameAnnotationArraysForSorting(
@@ -261,6 +264,8 @@ function typeAnnotationArbitraryOrder(annotation: CompleteTypeAnnotation) {
return 14;
case 'ObjectTypeAnnotation':
return 15;
case 'BooleanLiteralTypeAnnotation':
return 16;
case 'StringLiteralUnionTypeAnnotation':
return 17;
case 'StringTypeAnnotation':
@@ -17,6 +17,7 @@ import type {
TypeComparisonError,
} from './ComparisonResult';
import type {
BooleanLiteralTypeAnnotation,
CompleteReservedTypeAnnotation,
CompleteTypeAnnotation,
EventEmitterTypeAnnotation,
@@ -237,6 +238,12 @@ export function compareTypeAnnotation(
EQUALITY_MSG,
);
return compareNumberLiteralTypes(newerAnnotation, olderAnnotation);
case 'BooleanLiteralTypeAnnotation':
invariant(
olderAnnotation.type === 'BooleanLiteralTypeAnnotation',
EQUALITY_MSG,
);
return compareBooleanLiteralTypes(newerAnnotation, olderAnnotation);
case 'StringLiteralUnionTypeAnnotation':
invariant(
olderAnnotation.type === 'StringLiteralUnionTypeAnnotation',
@@ -907,6 +914,21 @@ export function compareStringLiteralTypes(
);
}
export function compareBooleanLiteralTypes(
newerType: BooleanLiteralTypeAnnotation,
olderType: BooleanLiteralTypeAnnotation,
): ComparisonResult {
return newerType.value === olderType.value
? {status: 'matching'}
: makeError(
typeAnnotationComparisonError(
'Boolean literals are not equal',
newerType,
olderType,
),
);
}
export function compareStringLiteralUnionTypes(
newerType: StringLiteralUnionTypeAnnotation,
olderType: StringLiteralUnionTypeAnnotation,
@@ -905,6 +905,30 @@ describe('compareTypes on string literals', () => {
});
});
describe('compareTypes on boolean literals', () => {
it('matches literals that are the same', () => {
expect(
compareTypes(
nativeTypeDiffingTypesMethodParamLookup('booleanLiteral0'),
nativeTypeDiffingTypesMethodParamLookup('booleanLiteral0'),
nativeTypeDiffingTypesAliases,
nativeTypeDiffingTypesAliases,
).status,
).toBe('matching');
});
it('fails on literals that are not the same', () => {
expect(
compareTypes(
nativeTypeDiffingTypesMethodParamLookup('booleanLiteral0'),
nativeTypeDiffingTypesMethodParamLookup('booleanLiteral1'),
nativeTypeDiffingTypesAliases,
nativeTypeDiffingTypesAliases,
),
).toHaveErrorWithMessage('Boolean literals are not equal');
});
});
describe('compareTypes on numeric literals', () => {
it('matches literals that are the same', () => {
expect(
@@ -17,6 +17,9 @@ import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboMo
type StringLiteral0 = 'foo';
type StringLiteral1 = 'bar';
type BooleanLiteral0 = true;
type BooleanLiteral1 = false;
type NumericLiteral0 = 1;
type NumericLiteral1 = 2;
@@ -170,6 +173,9 @@ export interface Spec extends TurboModule {
+stringLiteral0: (a: StringLiteral0) => void;
+stringLiteral1: (a: StringLiteral1) => void;
+booleanLiteral0: (a: BooleanLiteral0) => void;
+booleanLiteral1: (a: BooleanLiteral1) => void;
+numericLiteral0: (a: NumericLiteral0) => void;
+numericLiteral1: (a: NumericLiteral1) => void;