Home > Enterprise >  Errors in call method in typescript
Errors in call method in typescript

Time:02-28

I am a beginer in typescript and trying to impliment this piece of code but getting 2 errors in line number 3. It is showing name and address not defined. Can anybody help me to make this code workable.

var company = {
    fullName: function(ceo :string, teamcount :string) :string {
        return "Name :- "   this.name   "\nAddress:- "   this.address   "\nCEO :- "   ceo   "\nTeamCount :- "   teamcount;
        }
    }

var company1 = {
    name:"BOB the builder",
    address: "Disney world"
}


const temporaryVariable = company.fullName.call(company1,"BOB","30")
console.log(temporaryVariable)

CodePudding user response:

As per definition

The call() method calls a function with a given this value and arguments provided individually.

this is the current context and can be used to extend the properties, but you can't pass an object in its place.

The easiest way to do is to add the object as a param in your function like

var company = {
    fullName: function(data: any,ceo :string, teamcount :string) :string {
        return "Name :- "   data.name   "\nAddress:- "   data.address   "\nCEO :- "   ceo   "\nTeamCount :- "   teamcount;
        }
    }

and then update your call to below

const temporaryVariable = company.fullName.call(this, company1, "BOB","30");

CodePudding user response:

var company = {
fullName: function(ceo :string, teamcount :string) :string {
    return "Name :- "   this.name   "\nAddress:- "   this.address   "\nCEO :- "   ceo   "\nTeamCount :- "   teamcount;
    }
}

In this object, fullName needs to have access to the name and address variable at any time no matter in which context you are calling it.

And as the company object doesn't have those variables, this will try to look immediate next scope to find those variables, which may or may not exist, and that's why TS complains about that [Remember you may not want to break the code during run time, since name and address may not be found].

For example: In both below cases, Even if TS shows errors, it does work fine for your code, check logs of this

However, it doesn't work when you run it in this case

So, use the TS for the purpose it is supposed to use (Prevent bugs before run time)

You may want to refactor it like this

 var company = {
    fullName: function({name, address, ceo, teamcount}: {
        name: string;
        address: string;
        ceo: string;
        teamcount: string;
    }): string {
        return "Name :- "   name   "\nAddress:- "   address   "\nCEO :- "   ceo   "\nTeamCount :- "   teamcount;
        }
    }

var company1 = {
    name:"BOB the bulider",
    address: "Disney world"
}

// Works
const temporaryVariable1 = company.fullName({...company1, ceo:"BOB", teamcount: "30"})
console.log(temporaryVariable1)

// Example to show how it prevent bug
var company2 = {
    test:"BOB the bulider",
    address: "Disney world"
}

// TS complains, so you know you can't pass this objectto fullName function
const temporaryVariable2 = company.fullName({...company2, ceo:"BOB", teamcount: "30"})

console.log(temporaryVariable2)

NOTE Even though the accepted answer works fine, it defeats the purpose of using typescript. The function is still bugy.

  • Related