头图

Let's look at a concrete example. Here's a SAP UI5 Component written in TypeScript:

import UIComponent from "sap/ui/core/UIComponent";

/**
 * @namespace ui5.typescript.helloworld
 */
export default class Component extends UIComponent {

    public multiply(x : number, y : number) : number {
        return x * y;
    }
}

The first line of import UIComponent, where does its type definition come from?

If we left click on "sap/ui/core/UIComponent", we will see a declare module declaration.

After clicking, you can find a sap.ui.core.d.ts file under openui5 in the @types folder under the node_modules folder:

This is called a DefinitelyTyped external type definition file.

How to install external type definition files for UI5 for TypeScript:

npm install --save @types/openui5

Address: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/openui5

These external definitions are generated from OpenUI5 JSDoc. If something goes wrong, you need to fix the generator or JSDoc in the original OpenUI5 repository, not the definition files in this repository.

OpenUI5 type definitions are published under two npm package names:

  • @openui5/ts-types-esm (published directly by the UI5 development team)
  • @types/openui5 (maintained via DefinitelyTyped)

The difference between the two:

  • For those type definitions at @openui5/ts-types-esm, whenever a new patch version of OpenUI5 is released, a new patch version is released. Even if the type definition has not changed. This means that code and type definitions are completely in sync when using the exact same version.

However, for @types/openui5, DefinitelyTyped adopts the versioning method of definitelyTyped:

Only major and minor version numbers are aligned between library packages and type declaration packages.
The patch version of the type declaration package is independent of the library patch version.

The reasoning behind this is that with semantic versioning, the API will remain the same for all patch versions of the same major/minor version. For example: There are no API changes between OpenUI5 1.90.0 and OpenUI5 1.90.8. Therefore, there is no need to release new type definitions for OpenUI5 1.90.8 (and 1.90.7, 1.90.6, etc.).

The only exception is that we may still create new patch versions on DefinitiveTyped when there are major improvements or fixes in the documentation or definition generators.

More Jerry's original articles, all in: "Wang Zixi":


注销
1k 声望1.6k 粉丝

invalid