Can't call a setter function in typescript if the type is specified

I’m developing a frontend project, and right now I’m in the signup form, and I exported the following class for the user:

export class User {

    private _id: string
    private _name: string
    private _last_name: string
    private _email: string
    private _age: number
    private _password: string

    constructor(id: string, name: string, lastName: string, email: string, age: number, password: string) {
        this._id = id
        this._name = name
        this._last_name = lastName
        this._email = email
        this._age = age
        this._password = password
    }

    static emptyUser() {
        return new User('', '', '', '', 0, '')
    }

    public get id() : string {
        return this._id
    }

    public set id(id:string) {
        this._id = id
    }
    
    public get name() : string {
        return this._name
    }

    public set name(name:string) {
        this._name = name
    }

    public get lastName() : string {
        return this._last_name
    }

    public set lastName(lastName:string) {
        this._last_name = lastName
    }
    
    public get email() : string {
        return this._email
    }

    public set email(email:string) {
        this._email = email
    }
    
    public get age() : number {
        return this._age
    }

    public set age(age:number) {
        this._age = age
    }
    
    public get password() : string {
        return this._password
    }

    public set password(password:string) {
        this._password = password
    }
}

Then, whenever I call a setter fuction in my component, vscode signs an error due to the type "String" not having a signature. But if I change the setter parameter type to "any" the bug goes away. The component code goes bellow:

import { useState } from "react"
import { User } from "../../../core/User"
import AuthenticationButton from "../AuthenticationButton"
import AuthenticationInput from "../AuthenticationInput"

function SignUpForm() {

    const [clientSignUp, setClientSignUp] = useState<User>(User.emptyUser())

    function setClientName(name:string) {
        setClientSignUp(clientSignUp.name(name))
    }

    return (
        <div className="flex flex-col gap-3
                        text-xl">
            <h1 className="text-3xl">Sign Up</h1>
            <hr className="bg-gray-900 border border-gray-900" />
            <label htmlFor="SignUpName">Name:</label>
            <AuthenticationInput
                inputId="SignUpName"
                inputType="text"
            />
            <label htmlFor="SignUpLastName">Last name:</label>
            <AuthenticationInput
                inputId="SignUpLastName"
                inputType="text"
            />
            <label htmlFor="SignUpEmail">E-mail:</label>
            <AuthenticationInput
                inputId="SignUpEmail"
                inputType="email"
            />
            <label htmlFor="SignUpPassword">Password:</label>
            <AuthenticationInput
                inputId="SignUpPassword"
                inputType="password"
            />
            <div className="flex justify-center">
                <AuthenticationButton>Sign up</AuthenticationButton>
            </div>
        </div>
    )
}

export default SignUpForm

>Solution :

You are using setter in wrong way. Update your setClientName as

function setClientName(name: string) {
    clientSignUp.name = name;
    setClientSignUp(clientSignUp);
}

Leave a Reply