Tools to generate Dart interfaces from TypeScript declaration files and Web IDL definitions.
The tools to generate bindings are written in Dart, compiled to JavaScript, and run on Node.
There is one entrypoint present in this package:
js_interop_gen.dart: This entrypoint is for generating Dart interfaces from TS Declaration code, given the path to a .d.ts fileThe js_interop_gen requires Node.js v22 or newer. Older Node versions are not supported and may result in runtime failures.
To generate Dart interfaces for a given .d.ts file, run the following at the root of this package:
dart run js_interop_gen <input.d.ts> dart run js_interop_gen -o <output> <input>
If multiple files are passed, the output option is regarded as an output directory instead.
For more information on the command-line options you can pass alongside, you can check the help information
dart run js_interop_gen --help
The generator also has support for configuring the output of the generator, allowing support for configuring features like: variadic argument count, preamble (if any), TS Compiler Options, etc.
These configuration options can either be passed from the command line, or via a YAML configuration file. To pass a configuration file, pass the --config option.
Given a sample configuration file:
input: a.d.ts output: b.d.ts ts-config-file: tsconfig.json
The following are equivalent
dart run js_interop_gen -o b.d.ts --ts-config tsconfig.json a.d.ts dart run js_interop_gen --config config.yaml
Note that not all configuration options are direct mappings between the CLI and the configuration file.
| Option | Description | Example |
|---|---|---|
name | The name of the bindings | name: MyBindings |
description | A description of the bindings (optional) | description: My awesome bindings |
preamble | Preamble text to insert before the bindings (optional) | preamble: | // Generated. Do not edit. |
input | A file (single string) or set of files (array of strings) passed into the generator | input: bindings.d.ts or input: - bindings.d.ts |
output | The output file or directory to write the bindings to | output: lib/src/js |
include | Declarations to include in the generated output (as a list). Can either be passed as a raw string to match the full name, or as a regular expression. By default, the generator outputs all exported declarations | include: - myNumber |
language_version | The Dart Language Version to use, usually for formatting (optional) | language_version: 3.6.0 |
ts_config | An object consisting of TS Configurations following the tsconfig.json file schema used for configuring the TypeScript Program/Compiler (optional) | ts_config: compilerOptions: target: es2020 |
ts_config_file | The TS Configuration file (tsconfig.json) if any (optional) | ts_config_file: tsconfig.json |
generate_all | Include generating declarations for code that isn't exported. Defaults to false | generate_all: true |
ignore_errors | Ignore source code warnings and errors (they will still be printed). Defaults to false | ignore_errors: true |
functions.varargs | The number of arguments that variable-argument functions should take. Defaults to 4 | functions: varargs: 6 |
The generator scripts use a number of conventions to consistently handle TS definitions:
external declarations annotated with @JS.externalJSObjectimplements between extension typescall declarations, construct signatures, getters and setters.@redeclare.var declarations.JSObject with static members.var declarations, and functions.js_interop types.never type returned from a const declaration, function, readonly property or method has the declaration annotated with @doNotStore.package:web typesnull or undefined equals neverJSFunction overriding the call method. This allows for a stronger type signature than just JSFunction. While closures leave their calls as external, construct signatures just redirect to the class constructor.JSAny by default.typeof declarations are references to the type of the declaration, which are:keyof correctly).JSFunctionkeyof declarations are represented as unions of the keys for the operand type.@deprecated and @experimental