TypeScript Code Generation for React Native Turbo Module
npm install react-native-tscodegenreact-native-tscodegen-types to access these new APIs.
./src/turboModule.ts.
typescript
import { TurboModule, TurboModuleRegistry } from 'react-native-tscodegen-types';
export interface Spec extends TurboModule {
getHello(name: string): string;
}
// tslint:disable-next-line
export default (TurboModuleRegistry.getEnforcing('SampleTurboModule') as Spec);
`
Code generation
In order to do code generation, you need an extra JSON file for this TypeScript file.
They are one-to-one mapping for now,
if you have multiple TypeScript files, you need multiple JSON files
This will be improved in the future.
Name the following file as react-native-tscodegen.json
`json
{
"libraryName": "PlaygroundModule",
"outputDirectory": "./lib/cpp-generated",
"moduleSpecName": "PlaygroundModuleSpec",
"generators": [
"descriptors",
"events",
"props",
"tests",
"shadow-nodes",
"modulesAndroid",
"modulesCxx",
"modulesIOS"
],
"inputFile": "./src/turboModule.ts"
}
`
And add this script to your package.json
`json
{
"scripts": {
"codegen": "react-native-tscodegen ./react-native-tscodegen.json"
}
}
`
After running npm run codegen, you will see files get generated in ./lib/cpp-generated`.