2

I'm sure this is simple code, but I run into weird error but has no answer to. I did also look up the answer from this: get and set in TypeScript

But my code is exact same thing. But I have this error when I set the instance's property:

This expression is not callable.
  Type 'String' has no call signatures

This is my code

/**
 * Class that use underscore name and get set syntax
 */
class Person {

    // can give any property name, use convention `_`
    private _fname: string;
    private _lname: string;

    constructor(first: string, last: string) {
        this._fname = first;
        this._lname = last;
    }

    public get firstname(): string {
        return this._fname;
    }

    public set firstname(first: string) {
        this._fname = first;
    }

    public get lastname(): string {
        return this._lname;
    }

    public set lastname(last: string) {
        this._lname = last;
    }

}

// create instance
let character = new Person("Laila", "Law-Giver");
console.log(`Jarl of Riften is ${character.firstname} ${character.lastname}`);

character.firstname("Saerlund");
console.log(`${character.firstname} ${character.lastname} is her son, who sides with the Empire.`);

I have error on character.firstname("Saerlund");

What does this mean? I don't see what is wrong with the code.

2
  • 4
    You've defined it as a setter, so use it as a property: character.firstname = "name" Commented Mar 14, 2021 at 15:14
  • 1
    As a self taugh / newbie, I really appreciate this. Thank you! Commented Mar 14, 2021 at 16:19

1 Answer 1

6

You declared firstname as a setter and in order to assign a new value to it you should do as follows:

character.firstname = "Saerlund";
Sign up to request clarification or add additional context in comments.

1 Comment

As a self taugh / newbie, I really appreciate this. Thank you!

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.