Getting syntext error while define a class with private member


#1
export class SingletonService
{
    static instance:SingletonService;
    static isCreating:Boolean = false;
    access_token: String;
    user_id: String;
    source_id: String;
    msgOffset: 0;
    pageSize: 0;
    sections : [];
    selectedSection : {};
    instituteStandardSection : String;
    instituteStandard : String;
    examId : null;
    student: {};
 
    constructor() {
        if (!SingletonService.isCreating) {
            throw new Error("You can't call new in Singleton instances!");
        }
    }
}

I am getting syntax error on line “static instance:SingletonService;”.
This is the working code with IONIC 2.0.0-beta.19, but it gives error after upgrading to latest build IONIC 2.0.0-beta.25.


Some stuff are not found, like filter()
#2

Can you copy the entire error, please? Maybe the missing semi colon…


#3

@carlosadrian I have updated the code.


#4

Any chance you’re feeding TypeScript to a setup not prepared to handle it?


#5

@mohammed2raja The code is valid in TypeScript, can you confirm if your project is a TypeScript or a JavaScript one?


#6

@iignatov its a JavaScript project created by using command : ionic start tutorial --v2


#7

The code above is a valid TypeScript code but it’s not a valid JavaScript code.

After the latest updates in Ionic CLI invalid JavaScript code is no longer supported:

You have to convert your code to JavaScript or you could use a workaround to allow invalid JS (not really recommended).


#8

@iignatov Thanks a lot.
But all my files are .js files. So I have rewrite code again in typescript?
I have not much experience in TypeScript.
Is there any way to use same .js files ?


#9

Actually it seems that your code is already in TypeScript (or at least the one that you pasted), so you will have to convert it to JavaScript. If you want to use the current code in a JavaScript project (acknowledging that it’s an invalid JavaScript code) then you have to made modifications to your build process to allow it. Check out the linked post for more details on how exactly to do it:


#10

It sure seems a lot easier to me to switch the project to using TypeScript and keep writing TypeScript.


#11

@rapropos That’s what I also thought at first. However taking into consideration that he doesn’t have much experience with TypeScript and that the state of the other code is unknown (i.e. only TypeScript or a mix of TypeScript and JavaScript) it’s hard to tell. While I personally prefer TypeScript, there’s definitely a lot to be desired in terms of library support (e.g. there are still plenty of missing or outdated type definitions).

@mohammed2raja I should say that I agree with @rapropos that you should consider if it’s not easier to switch the project to TypeScript. If the code is already mostly in TypeScript then it might be easier to go this way. TypeScript is not that hard - here’s a good article about it. And it might be a good investment because Ionic 2 is moving in this direction. Also I would recommend you to weigh your priorities and constraints and decide which option would be the better one in your case.


#12

Fair enough. Since TypeScript is largely a superset of JavaScript, though, I still think that tsc is going to do a better job of coping with a mishmash of TypeScript and JavaScript than would somebody without much TS knowledge trying to transcode it down to JS by hand.


#13

I agree that there shouldn’t be any problems with the JavaScript code itself - I was actually thinking about the Angular/Ionic specifics for JavaScript (e.g. static get parameters() for dependency injection, etc). However, I guess that when the code worked well while the TypeScript compiler was used for transpilation under the hood, it might work fine this way (or with minimal changes) in TypeScript too. Then it maybe depends only on the support for TS projects of the libraries used in the project (e.g. Auth0 doesn’t play well with TS).