Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support TypeScript type (T) for turbo module codegen (module only) #34621

Closed
wants to merge 4 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';

export interface Spec extends TurboModule {
readonly getArray: (arg: Array<string>) => Array<string>;
readonly getArray: (arg: ReadonlyArray<string>) => ReadonlyArray<string>;
readonly getArray: (arg: Array<string>) => (Array<(string)>);
readonly getArray: (arg: ReadonlyArray<string>) => (ReadonlyArray<(string)>);
}

export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
Expand All @@ -336,8 +336,8 @@ import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';

export interface Spec extends TurboModule {
readonly getArray: (arg: string[]) => string[];
readonly getArray: (arg: readonly string[]) => readonly string[];
readonly getArray: (arg: string[]) => ((string)[]);
readonly getArray: (arg: readonly string[]) => (readonly (string)[]);
}

export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ function translateTypeAnnotation(
resolveTypeAnnotation(typeScriptTypeAnnotation, types);

switch (typeAnnotation.type) {
case 'TSParenthesizedType': {
return translateTypeAnnotation(
hasteModuleName,
typeAnnotation.typeAnnotation,
types,
aliasMap,
tryParse,
cxxOnly,
);
}
case 'TSArrayType': {
return translateArrayTypeAnnotation(
hasteModuleName,
Expand Down
38 changes: 19 additions & 19 deletions packages/react-native-codegen/src/parsers/typescript/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ export type TypeDeclarationMap = {[declarationName: string]: $FlowFixMe};

function getTypes(ast: $FlowFixMe): TypeDeclarationMap {
return ast.body.reduce((types, node) => {
if (node.type === 'ExportNamedDeclaration' && node.exportKind === 'type') {
if (
node.declaration.type === 'TSTypeAliasDeclaration' ||
node.declaration.type === 'TSInterfaceDeclaration'
) {
types[node.declaration.id.name] = node.declaration;
switch (node.type) {
case 'ExportNamedDeclaration': {
if (node.declaration) {
switch (node.declaration.type) {
case 'TSTypeAliasDeclaration':
case 'TSInterfaceDeclaration':
case 'TSEnumDeclaration': {
types[node.declaration.id.name] = node.declaration;
break;
}
}
}
break;
}
case 'TSTypeAliasDeclaration':
case 'TSInterfaceDeclaration':
case 'TSEnumDeclaration': {
types[node.id.name] = node;
break;
}
} else if (
node.type === 'ExportNamedDeclaration' &&
node.exportKind === 'value' &&
node.declaration &&
node.declaration.type === 'TSEnumDeclaration'
) {
types[node.declaration.id.name] = node.declaration;
} else if (
node.type === 'TSTypeAliasDeclaration' ||
node.type === 'TSInterfaceDeclaration' ||
node.type === 'TSEnumDeclaration'
) {
types[node.id.name] = node;
}
return types;
}, {});
Expand Down