0

I have an old project which is written in JavaScript and I've "converted" it to TypeScript by simply renaming it to .ts.

Everything works fine, but I don't know how to assign the type for the following example:

function test(userid: number) {
    var that = this;
    this.UserId = userid;
    //this.UserId : number = userid;   //Not Working
}

test.prototype.load = () => {
     var id = this.UserId;
}

I don't know how to assign the TypeDefinition to the local UserId variable. This is just a simple example. My project is very large and I can't refactor the whole thing.

1

3 Answers 3

1

I'm not aware of a way how to assign type to this.UserId. I don't think, you can do it because that's what classes are for.

In TypeScript you would write:

class test {
    public UserId: number;

    constructor(userid: number) {
        this.UserId = userid;
    }

    public load = () => {
        let id = this.UserId;
    }
}

which translates to:

var test = (function () {
    function test(userid) {
        var _this = this;
        this.load = function () {
            var id = _this.UserId;
        };
        this.UserId = userid;
    }
    return test;
})();

If you don't have resources to rewrite your app, then I would simply go with:

function test(userid: number) {
    this.UserId = userid; // Don't add type here ...
}

test.prototype.load = () => {
    var id = this.UserId as number; // ... add type here
}

It's not that powerful but it's better than nothing.

Sign up to request clarification or add additional context in comments.

Comments

0

For a small amount of effort, the compiler enforces typing and you get code completion if you define an interface and hide a cast in a function call (that I have called typed() here). Wrap any typed uses of this with that function.

interface Test {
    UserId: number;
}

function typed(test: any): Test {
    return <Test>test;
}

function test(userid: number) {
    var that = typed(this);
    that.UserId = userid;
    var result: number = that.UserId; // Compiles without errors
    that.UserId.toFixed(); // Compiles without errors
}

test.prototype.load = () => {
    var id = typed(this).UserId;
}

It may be a good half-way house.

Comments

0

First of all, don't use an arrow function for the load method. Use a function expression:

function test(userid: number) {
    this.UserId = userid;
}

test.prototype.load = function() {
     var id = this.UserId; // now this will work
};

An arrow function doesn't bind to its own this value, but uses the this value in the parent scope. So in a web browser, the code with the arrow function would use Window, but with a function expression like shown here, it will use the instance of test. This is the same in JavaScript, so maybe the use of an arrow function was an addition to the code?

If you want to start adding type information, you might as well change the code to be an ES6 class. It's less work in the long run and it's quite simple to do over time. Here's an example:

class test {
    constructor(public UserId: number) {
    }

    load() {
        var id = this.UserId; // id is typed as number
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.