Giter VIP home page Giter VIP logo

ui5ts's Introduction

This Repo Is No Longer Maintained

For a newer version supporting a more recent version of UI5, see https://github.com/neoprincie/ui5ts.

ui5ts

A simple adapter to develop SAPUI5 and OpenUI5 applications using TypeScript and ES2015 modules/classes

Release Notes

0.3.2 - Handle some UI5 API errors in the Docs, when method with names like getSomething has void as return type. It is being replaced by any now.

0.3.1 - Just fixed an error in this README instructions.

0.3.0 - Create definitions for more than one version of UI5 (just 1.46 and 1.48 for now). Published in https://github.com/lmcarreiro/ui5-typescript-definitions too. Be carefull, now that there is these definitions inside the package and I have plans to make these definitions better (normally replacing an any type with a more specific one), almost all new versions from now on may be a breaking change.

0.2.0 - Create my own UI5 definitions generator, because the available ones didn't fit my needs (you can still use another, just set it up in your tsconfig.json).

0.1.* - Generated exports files for all namespace sap.* objects, to make possible import these objects without creating a single <object>.d.ts for each imported object.

0.0.* - Just a draft.

How to use

It is very simple, make it work with only 4 steps:

  1. Install ui5ts, typescript and @types/jquery npm packages
  2. Add a reference to the "library" in your index.html
  3. Add the required TypeScript options in the tsconfig.json
  4. Change your <class-name>.js to a <class-name>.ts

Check this Master-Detail example app https://github.com/lmcarreiro/ui5-typescript-example that is already working with ui5+typescript.

1) Install ui5ts, typescript and @types/jquery npm packages

npm install @types/jquery --save-dev
npm install typescript --save-dev
npm install ui5ts --save

2) Add a reference to the library in your index.html

Put a reference to the ui5ts.js script in your index.html file using a script tag <script src="node_modules/ui5ts/ui5ts.js" type="text/javascript"></script> between the sap-ui-core.js script tag and the sap.ui.getCore().attachInit() call:

...
<!-- Bootstrapping UI5 -->
<script id="sap-ui-bootstrap" src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js" ... ></script>

+ <!-- Convert typescript generated modules/classes into ui5 modules/classes -->
+ <script type="text/javascript" src="node_modules/ui5ts/ui5ts.js"></script>

<script>
    sap.ui.getCore().attachInit(function () { ... });
</script>
...

3) Add the required TypeScript options in the tsconfig.json

  • compilerOptions.module = "amd"
  • compilerOptions.experimentalDecorators = true (just to avoid typescript warning/error)
  • compilerOptions.baseUrl = "./" (your project root, if you change this value, you need to change the paths too)
  • compilerOptions.paths = { ... } (your paths, relative to your baseUrl, check the example bellow)
  • files = ["node_modules/ui5ts/ui5ts.d.ts", "node_modules/ui5ts/ui5-types/1.48/sap.d.ts", "node_modules/ui5ts/ui5-types/1.48/jQuery.d.ts", ...]

Example of tsconfig.json file:

{
    "compilerOptions": {
        "target": "es5",
        "module": "amd",
        "experimentalDecorators": true,
        "alwaysStrict": true,
        "noImplicitAny": true,
        "strictNullChecks": true,
        "noImplicitReturns": true,
        "noImplicitThis": true,
        "sourceMap": true,
        "baseUrl": "./",
        "paths": {
            "your/app/namespace/*": [ "./src/*" ],
            "sap/*": [ "./node_modules/ui5ts/exports/sap/*" ]
        }
    },
    "files": [
        "node_modules/ui5ts/ui5ts.d.ts",
        "node_modules/ui5ts/ui5-types/1.48/sap.d.ts",
        "node_modules/ui5ts/ui5-types/1.48/jQuery.d.ts"
    ],
    "include": [
        "src/**/*",
        "node_modules/@types"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

4) Change your <class-name>.js to a <class-name>.ts

UI5 JavaScript way:

sap.ui.define([
    "sap/ui/core/UIComponent",
    "typescript/example/ui5app/model/models"
], function (UIComponent, models) {
    "use strict";
    
    return UIComponent.extend("typescript.example.ui5app.Component", {
        metadata: {
            manifest: "json"
        },
        
        init: function () {
            // set the device model
            this.setModel(models.createDeviceModel(), "device");
            // call the base component's init function and create the App view
            UIComponent.prototype.init.call(this);
            // create the views based on the url/hash
            this.getRouter().initialize();
        }
    });
});

ES2015 TypeScript way:

import UIComponent  from "sap/ui/core/UIComponent";
import models       from "typescript/example/ui5app/model/models";

@UI5("typescript.example.ui5app.Component")
export default class Component extends UIComponent
{
    public static metadata: any = {
        manifest : "json"
    };

    public init(): void {
        // set the device model
        this.setModel(models.createDeviceModel(), "device");
        // call the base component's init function and create the App view
        super.init();
        // create the views based on the url/hash
        this.getRouter().initialize();
    }
}

Don't forget

  • You need to decorate your class with @UI5("your.full.namespace.ClassName"), this decorator parameter will be passed to BaseClass.extend("your.full.namespace.ClassName", { ... }); call at runtime.
  • You need to export your class as default export.
  • If your class has the ui5 metadata object, define it as static
  • The paths in the import statements must be the same as it would be if you were using sap.ui.define() function. The TypeScript compiler will generate an AMD module with a define() call with these paths, and the define() function that ui5ts overrides will call the real sap.ui.define() function. This is the way that ui5ts works.
  • If your/app/namespace/is/too/big, you don't need to have all this levels of directories in your physical project structure, you can create a virtual mapping using the tsconfig.json configuration option paths (see it in the common problems bellow).

Resolving common typescript errors and module resolution problems

Problem: Doesn't find the @UI5 decorator:

...
// error TS2304: Cannot find name 'UI5'.
@UI5("your.full.namespace.ClassName")
export default class Component extends UIComponent {
...

Solution: Make sure you have the ui5ts.d.ts referenced in your tsconfig.json

...
"files": [
    ...,
+   "node_modules/ui5ts/ui5ts.d.ts"
],
...

Problem: Doesn't find your own *.ts class:

...
// error TS2307: Cannot find module 'your/app/namespace/folder/ClassName'.
import ClassName from "your/app/namespace/folder/ClassName";
...

Solution: Make sure you have the path of your namespace root in the tsconfig.json and if it match with your application startup in the index.html

...
"compilerOptions": {
    ...
    "baseUrl": "./",
    "paths": {
        ...
+       "your/app/namespace/*": [ "./src/*" ]
    }
...
...
sap.ui.getCore().attachInit(function () {
    sap.ui.require(["sap/m/Shell", "sap/ui/core/ComponentContainer"], function (Shell, ComponentContainer) {
        new Shell({
            app: new ComponentContainer({
                height : "100%",
+               name : "your.app.namespace"
            })
        }).placeAt("content");
    });
});
...

Problem: Doesn't find your own *.js class:

...
// error TS2307: Cannot find module 'your/app/namespace/folder/ClassName'.
import ClassName from "your/app/namespace/folder/ClassName";
...

Solution: Create a corresponding *.d.ts of your *.js class or forget about it. You can live with this error and the app will still work. Even if you allow the TypeScript compiler to accept *.js modules, it will not recognize it as a AMD module, since you declare it using sap.ui.define() instead of define().

Problem: Doesn't find a class in sap.* namespace:

...
// error TS2307: Cannot find module 'sap/ui/core/UIComponent'.
import UIComponent from "sap/ui/core/UIComponent";
...

Solution: Make sure you have mapped the ui5ts exports folder in your paths of the tsconfig.json:

...
"compilerOptions": {
    ...
    "baseUrl": "./",
    "paths": {
        ...
+       "sap/*": [ "./node_modules/ui5ts/exports/sap/*" ]
    }
...

If the problem still remains, please, create an issue in the github project:

https://github.com/lmcarreiro/ui5ts/issues

ui5ts's People

Contributors

lmcarreiro avatar neoprincie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ui5ts's Issues

trying to run it in the powerbi framework

i want to use this inside the powerbi framework and compiler but i wont compile at all.
1st : error TYPESCRIPT C:/Work/PowerBI/test/node_modules/ui5ts/ui5-types/1.46/sap.d.ts : (110122,87) Cannot find name 'Promise'. error TYPESCRIPT C:/Work/PowerBI/test/node_modules/ui5ts/ui5-types/1.46/sap.d.ts : (110468,27) In ambient enum declarations member initializer must be constant expression. error TYPESCRIPT C:/Work/PowerBI/test/node_modules/ui5ts/ui5-types/1.46/sap.d.ts : (110472,30) In ambient enum declarations member initializer must be constant expression. error TYPESCRIPT C:/Work/PowerBI/test/node_modules/ui5ts/ui5-types/1.46/sap.d.ts : (111496,24) In ambient enum declarations member initializer must be constant expression. error TYPESCRIPT C:/Work/PowerBI/test/node_modules/ui5ts/ui5-types/1.46/sap.d.ts : (111500,25) In ambient enum declarations member initializer must be constant expression. error TYPESCRIPT C:/Work/PowerBI/test/node_modules/ui5ts/ui5-types/1.46/sap.d.ts : (111504,23) In ambient enum declarations member initializer must be constant expression. error TYPESCRIPT C:/Work/PowerBI/test/node_modules/ui5ts/ui5-types/1.46/sap.d.ts : (111508,26) In ambient enum declarations member initializer must be constant expression. error TYPESCRIPT C:/Work/PowerBI/test/node_modules/ui5ts/ui5-types/1.46/sap.d.ts : (111844,26) Class 'AnalyticalTable' incorrectly extends base class 'Table'. Types of property 'addSelectionInterval' are incompatible. Type '(iFromIndex: number, iToIndex: number) => AnalyticalTable' is not assignable to type '(iIndexFrom: number, iIndexTo: number) => Table'. Type 'AnalyticalTable' is not assignable to type 'Table'. Types of property 'setSelectionBehavior' are incompatible. Type '(sBehavior: string) => Table' is not assignable to type '(sSelectionBehavior: SelectionBehavior) => Table'. Types of parameters 'sBehavior' and 'sSelectionBehavior' are incompatible. Type 'SelectionBehavior' is not assignable to type 'string'. error TYPESCRIPT C:/Work/PowerBI/test/node_modules/ui5ts/ui5-types/1.46/sap.d.ts : (112997,25) In ambient enum declarations member initializer must be constant expression. error TYPESCRIPT C:/Work/PowerBI/test/node_modules/ui5ts/ui5-types/1.46/sap.d.ts : (113001,37) In ambient enum declarations member initializer must be constant expression. error TYPESCRIPT C:/Work/PowerBI/test/node_modules/ui5ts/ui5-types/1.46/sap.d.ts : (113005,28) In ambient enum declarations member initializer must be constant expression. error TYPESCRIPT C:/Work/PowerBI/test/node_modules/ui5ts/ui5-types/1.46/sap.d.ts : (113009,26) In ambient enum declarations member initializer must be constant expression. error TYPESCRIPT C:/Work/PowerBI/test/node_modules/ui5ts/ui5-types/1.46/sap.d.ts : (113013,37) In ambient enum declarations member initializer must be constant expression. error TYPESCRIPT C:/Work/PowerBI/test/node_modules/ui5ts/ui5-types/1.46/sap.d.ts : (113017,27) In ambient enum declarations member initializer must be constant expression. error TYPESCRIPT C:/Work/PowerBI/test/node_modules/ui5ts/ui5-types/1.46/sap.d.ts : (113021,30) In ambient enum declarations member initializer must be constant expression. error TYPESCRIPT C:/Work/PowerBI/test/node_modules/ui5ts/ui5-types/1.46/sap.d.ts : (113033,29) In ambient enum declarations member initializer must be constant expression. error TYPESCRIPT C:/Work/PowerBI/test/node_modules/ui5ts/ui5-types/1.46/sap.d.ts : (113037,29) In ambient enum declarations member initializer must be constant expression. <-- this goes on for ever

2nd: in the sap.d.ts i am getting an error : The name "IterableIterator" wanst found

its here a way to fix that issue or is it almost impossible to run ui5 in typescript and the powerbi custom visual framework?

Autogenerated code is not handled

First let me say thank you for your module. It allows me to use types in many more places than I could without.

I'd like to point out, though, that the autogenerated methods for anything in metadata (properties, etc) are not handled and need to be duplicated in your own code to be accessible without type errors. While I don't think it can be done with the (unmodified) typescript compiler, I'd like to see it mentioned in the README file. If you have any idea how to handle metadata better or even automatically, I'd love to know.

JSONModel.getProperty return type tagged as void

Hi!,

It seems if you do this on a controller
var m = this.getView().getModel("a"); var some_string = m.getProperty("/someProp");
you'll get an error saying some_string is type void so can't do anything with it. Am I missing something? Was expecting getProperty to return "any" but this is not what's defined in the sap.d.ts.

Thanks a lot! This is a great project :)

Runtime call generation

I am currently looking for a way of leveraging TypeScript's advantages on UI5 applications and found your repo.

The only drawback on your method seems to be that the necessary code to call sap.ui.define is generated at runtime. Isn't there a way of generating the equivalent *.js file already with the right call to "sap.ui.define"?

Have you had other experiences using UI5 with TypeScript?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.