Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions types/react-native/Codegen.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ declare module 'react-native/Libraries/Utilities/codegenNativeCommands' {
readonly supportedCommands: ReadonlyArray<T>;
}

function codegenNativeCommands<T extends object>(
options: Options<keyof T extends string ? keyof T : never>,
): T;
function codegenNativeCommands<T extends object>(options: Options<keyof T extends string ? keyof T : never>): T;

export default codegenNativeCommands;
}
Expand All @@ -29,3 +27,35 @@ declare module 'react-native/Libraries/Utilities/codegenNativeComponent' {

export default codegenNativeComponent;
}

declare module 'react-native/Libraries/Types/CodegenTypes' {
import type { NativeSyntheticEvent } from 'react-native';

// Event types
// We're not using the PaperName, it is only used to codegen view config settings

export type BubblingEventHandler<T, PaperName extends string | never = never> = (
event: NativeSyntheticEvent<T>,
) => void | Promise<void>;
export type DirectEventHandler<T, PaperName extends string | never = never> = (
event: NativeSyntheticEvent<T>,
) => void | Promise<void>;

// Prop types
export type Double = number;
export type Float = number;
export type Int32 = number;
export type UnsafeObject = object;

type DefaultTypes = number | boolean | string | ReadonlyArray<string>;
// Default handling, ignore the unused value
// we're only using it for type checking
//
// TODO: (rickhanlonii) T44881457 If a default is provided, it should always be optional
// but that is currently not supported in the codegen since we require a default

export type WithDefault<Type extends DefaultTypes, Value extends Type | string | undefined | null> =
| Type
| undefined
| null;
}
1 change: 1 addition & 0 deletions types/react-native/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
// Sebastian Silbermann <https://github.com/eps1lon>
// Zihan Chen <https://github.com/ZihanChen-MSFT>
// Lorenzo Sciandra <https://github.com/kelset>
// Mateusz Wit <https://github.com/MateWW>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0

Expand Down
24 changes: 23 additions & 1 deletion types/react-native/test/fabric-component-sample.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
import codegenNativeComponent, { NativeComponentType } from 'react-native/Libraries/Utilities/codegenNativeComponent';
import codegenNativeCommands from 'react-native/Libraries/Utilities/codegenNativeCommands';
import {
WithDefault,
Double,
Float,
Int32,
UnsafeObject,
BubblingEventHandler,
DirectEventHandler,
} from 'react-native/Libraries/Types/CodegenTypes';
import type { ViewProps } from 'react-native';

type Event = Readonly<{
value: Double;
}>;

interface NativeProps extends ViewProps {
color?: string;
string?: string;
number?: number;
boolean?: boolean;
default?: WithDefault<'option1' | 'option2', 'option1'>;
double?: Double;
float?: Float;
int32?: Int32;
unsafeObject?: UnsafeObject;
onBubblingEventHandler?: BubblingEventHandler<Event>;
onDirectEventHandler?: DirectEventHandler<Event>;
}

export type SampleViewType = NativeComponentType<NativeProps>;
Expand Down