This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Expo Type generation reference
npm
A reference for the expo-type-information package.
The
expo-type-informationlibrary works only on macOS. Available for SDK 56 and later.
The expo-type-information package provides tools to automatically generate TypeScript interface for Swift modules. It consists of multiple parts:
- Swift parser based on
sourcekitten: a parser that can retrieve and structure the type information from a Swift Expo module. - Type Abstraction: an abstraction over type information relevant for Expo modules, the parser returns the information in this abstracted way.
- TypeScript AST Emitter: a set of functions which help with generating TypeScript code.
- CLI: a command-line tool, which integrates the above functionalities.
Configuration
Install the expo-type-information library:
To use this package you also need to have sourcekitten installed. You can use macOS package manager like Homebrew.
CLI reference
Common CLI options
Every CLI command in this library uses the following common options for configuration. The only exception is the inline-modules-interface which has slightly different options.
Main commands
module-interface
Generates a full TypeScript interface for a Swift module. It consists of:
- types.ts file with all types defined in the module
- module.ts with the native module definition
- view.tsx for each view defined in the module
- index.ts file which reexports some functions
Accepts standard Common CLI options.
inline-modules-interface
Creates a TypeScript interface for every Swift inline module in the project. The interface consists of two files:
- Module.generated.ts: This is regenerated with each run of the command
- Module.tsx: This one is not regenerated if you change it
Options:
short-module-interface
Creates a short TypeScript interface for an Expo module. Overwrites ModuleName.generated.ts and creates ModuleName.ts if not present. Can be used with inline-modules.
Accepts standard Common CLI options.
generate-mocks-for-file
Generates mocks for a given expo module.
Accepts standard Common CLI options.
Other commands
These commands are internal or very specific.
All commands in this section accept the standard Common CLI options.
other type-information
Parses Swift module type information and outputs a FileTypeInformation JSON.
other generate-module-types
Generates a type declaration file content for a module.
other generate-view-types
Generates a type declaration file for a native View.
other generate-jsx-intrinsics
Generates a declaration file for a View, updates JSX intrinsics with the View props.
other preprocess-file
Print the preprocessed file(s) in the state right before parsing them using sourcekitten. It helps with checking how the --module-path, --input-path, and --type-inference options affect the parsed file.
Type information abstraction
Used for testing purposes, maps Arrays to Sets and Maps depending on the field and returns FileTypeInformation object.
FileTypeInformationFileTypeInformation object.
Reads and extracts FileTypeInformation from either a provided file path or a raw string of source code.
If a raw string is provided, or if the PREPROCESS_AND_INFERENCE inference option is selected,
the function will create a temporary file with the (optionally preprocessed) content to facilitate parsing.
Promise<FileTypeInformation | null>A promise that resolves to a FileTypeInformation object if the input was parsed successfully. Otherwise, it resolves to null.
Used for testing purposes, maps Sets and Maps to Arrays and returns FileTypeInformationSerialized object which can be written to a JSON.
FileTypeInformationSerializeda FileTypeInformationSerialized object.
TypeScript generation
Generates a short TypeScript interface for an Expo module. This creates the content for two files: a volatile generated file containing raw type definitions, and a stable user-facing file that wraps and exports the native module methods in new functions.
Promise<{
moduleTypescriptInterfaceFileContent: string,
volatileGeneratedFileContent: string
}>A promise that resolves to an object containing the string contents for both the volatile generated file and the stable TypeScript interface file.
Generates a full, multi-file TypeScript interface for an Expo module. The generated interface is separated into a file with type definitions, a file which wraps the native module, a file for each view defined in a module and an index file which reexports all definitions from the other files.
Promise<{
indexFile: OutputFile,
moduleNativeFile: OutputFile,
moduleTypesFile: OutputFile,
moduleViewsFiles: OutputFile[]
} | null>A promise that resolves to an object containing the string contents for all of the generated files or null if the generation has failed.
Generates the TypeScript string content for a native View's type declaration file which mounts the View props on the global JSXIntrinsics.
Promise<string | null>A promise that resolves to a string containing the TypeScript declaration file content or null if the generation has failed.
Generates the TypeScript string content for a native module type declaration file.
Promise<string | null>A promise that resolves to a string containing the TypeScript module declaration file content or null if the generation has failed.
Generates the TypeScript string content for a native View's type declaration file.
Promise<string | null>A promise that resolves to a string containing the TypeScript declaration file content or null if the generation has failed.
Mock generation
This function generates JavaScript/TypeScript mocks for each provided FileTypeInformation object.
Promise<void>nothing
Promise<FileTypeInformation[]>Component
Type: React.Element<GetFileTypeInformationOptions>
Types
Literal type: union
Represents an anonymous type, a one that is not named instead written directly in the code, such as inline generics, arrays, or optionals.
Acceptable values are: ParametrizedType | SumType | OptionalType | DictionaryType | ArrayType
Type: Type
Represents a list or array of a specific type.
Note: The information that this type is array is implicit and exists only in the type system and on the parent type. There is no field on the
ArrayTypeobject that explicitly indicates that.
Retains information of where the thing was defined in the file. As collecting type information is written in asynchronous way it is non-deterministic. To make it deterministic we just sort the declaration by the definitionOffset, maintaining the same ordering as in original file.
Defines an input option for extracting type information from a set of physical files.
FileTypeInformation object abstracts over type related information in a file.
The abstraction is closely related to Typescript and expo NativeModules (both to be independent of the actual native side
and to give accurate information about what and how we can use the given module).
Serialized version of the FileTypeInformation, suitable for JSON storage or testing environments.
Options specifying the input source and inference level for retrieving type information.
Type: Type
Represents an optional type that can also resolve to null or undefined.
Note: The information that this type is optional is implicit and exists only in the type system and on the parent type. There is no field on the
OptionalTypeobject that explicitly indicates that.
Represents a parametrized type, that is a generic type with specified parameters e.g. Map<string, number>.
Defines an input option for extracting type information directly from a raw string of source code.
Type: Map<string, IdentifierDefinition>
Maps type identifier strings to their definition objects.
Enums
Represents a basic type that is not user defined.
Represents the kind of a parsed identifier from a native file.
Defines the level of type inference to apply when extracting type information.
Note: In case where type inference is on, it may take more than twice the time to compute the type information.
Categorizes the type node within the abstract syntax tree.
Swift parser limitations
expo-type-information library uses sourcekitten to parse the Swift file. Parsing a Swift file is a complex task and not everything is now implemented.
Known issues about the current version of parsing the Swift file
-
Nested classes will not be resolved fully.
Sourcekittenhas a limit on resolving nested closures, so classes defined in your module may not be fully resolved. This is the reason why if there is a DSLClassin a module, its methods will have their return typesunresolved. -
Return type resolution,
PREPROCESS_AND_INFERENCEinference option.When the return type of a closure is not provided explicitly, the tool needs to infer it. One of the ways of doing it in
sourcekittenis when there is areturn identifierstatement we can ask about the type of theidentifier. The third inference option,PREPROCESS_AND_INFERENCE, rewrites the file such that for eachreturn expressionstatement a newlet return_expression = expression; return return_expression;is inserted so that we can ask about the identifier type. Rewriting is known to sometimes have issues (mostly because of strings and comments) so it may not always work. There is also currently no way to resolve areturn-less return, when the return expression is a tail expression of the closure. -
Unsupported Expo Module DSL declarations.
Not all of the DSL declarations are parsed right now, and some are not parsed in every context. For example
Eventsare parsed inside aViewbut not in a Module definition. -
Using Unicode characters breaks
sourcekittenoffsets.