
@ui5/dts-Generator
This npm package is used for generating (and checking) TypeScript type definitions (*.d.ts
files) for SAPUI5/OpenUI5 libraries implemented in JavaScript, using their api.json
files as input.
These api.json
files for a UI5 library can be generated with the command ui5 build jsdoc
, executed within the root folder of the respective library. This works for your own custom libraries as well as for the original UI5 sources.
This command creates the api.json
file at dist/test-resources/<library_namespace_and_name>/designtime/api.json
(do not confuse this file with the api.json
file one folder below inside apiref
- that one is meant for the UI5 SDK).
For the original UI5 framework libraries, the resulting type definitions are published as @sapui5/types and @openui5/types.
The generator can produce both, the new "ES modules" version of the type definitions as well as the legacy "globals" version of the types, which is released as ts-types
but will be discontinued for UI5 2.x.
In addition to the generation, this package also provides means to check the generated *.d.ts
files in two ways:
- by compiling them with the TypeScript compiler and
- by running a
dtslint
check.
The latter is mainly done because it is required for publishing the resulting type definitions at DefinitelyTyped. The UI5 team only applies this check to the OpenUI5 libraries which are actually published there.
Details about the implementation of this package can be found in TECHNICAL.md.
Usage
Install the latest version via npm:
npm install @ui5/dts-generator --save-dev
You can then use the tool either from the command line as CLI or from your own NodeJS code using its APIs. Make sure to use at least version 3.x of the dts-generator, as its usage and API changed vastly compared to previous versions 2.x and below!
There is a complete example for using one of the APIs a few sections below. A simple CLI call looks like this:
npx @ui5/dts-generator <api_json_file> --targetFile <dts_target_file>
But in many cases you will want to pass additional arguments related to generation directives, paths of library dependencies etc.
The APIs
generate
: generate the *.d.ts
file for one UI5 library from its api.json
file (and optionally other files)generateFromObjects
: generate the *.d.ts
content as string for one UI5 library from its api.json
content given as JS object (and optionally other objects)generateFromPaths
: generate the *.d.ts
file for one UI5 library from its api.json
file (and optionally other directory paths)
Please see the TypeScript API for a detailed documentation and the respective arguments.
The CLIs
When started from the command line, the main file index.js
is an entry point for generating *.d.ts
files:
Usage:
node index.js <apiFile> <options>
With:
<apiFile> File path+name of the api.json file for the library for which the d.ts file should be generated.
<options>:
-h, --help Show this help message and exit
--dependenciesApiPath PATH
Directory where the api.json files are located for the libraries on which the currently to-be-built library depends.
--dependenciesDTSPathForCheck PATH
Directory where the d.ts files are located of the libraries on which the currently to-be-built library depends. Only needed for the
check.
--directivesPath PATH
Directory where the .dtsgenrc files for the libraries (current and dependencies) are located.
--targetFile FILE
File path and name of the target d.ts file to write.
--verbose Set when the console output should be verbose.
--skipCheckCompile Set when the test compilation should be skipped.
--dependenciesDTSPathForCheckForGlobals PATH
Directory where the d.ts files (using globals, not ES modules) are located of the libraries on which the currently to-be-built library
depends. Only needed when globals are generated and the check is run.
--targetFileForGlobals FILE
File path and name of the target d.ts file to write for the type defiitions with globals (not ES modules). Only needed when globals
should be generated.
The file runCheck.js
is used for triggering a check of the generated *.d.ts
files.
Usage:
node runCheck.js <dtsDir> [-h, --help] [--verbose] [--checkDtslint]
With:
<dtsDir> File path+name of the api.json file for the library for which the d.ts file should be generated.
<options>:
-h, --help show this help message and exit
--verbose Set when the console output should be verbose.
--checkDtslint Set when the test compilation should be skipped.
Usage Example generateFromObjects
import { generateFromObjects } from "@ui5/dts-generator";
const apiObject = {
"$schema-ref": "http://schemas.sap.com/sapui5/designtime/api.json/1.0",
version: "1.120.0",
library: "sap.dummy",
symbols: [],
};
const directives = {};
const result = generateFromObjects({
apiObject,
directives,
dependencyApiObjects: [
],
});
console.log(result.dtsText);
Support
For problems caused by the transformation process implemented in this dts-generator, please open issues in this repository on GitHub.
However, issues in the UI5 type definitions which are also present in the API documentation originate from the JSDoc comments in the original OpenUI5/SAPUI5 code, so please directly open an OpenUI5/SAPUI5 ticket in this case.
Contributing
See CONTRIBUTING.md.