1

Good evening! I am newbie in javascript and typescript.

I can't understand something. I carefully typed my code and the situation appeared. I can't imagine, why, through it's something basic. Look:

class A {
    id: number;
    constructor(id:number){
        this.id=4;
    }
    isIdZero?():boolean{
        if(this.id===0)
            return true;
        else 
            return false;
    }
}

let a : A= new A(0);
let b : A={
    id:9
}

alert(b.isIdZero());

This code makes an error "Cannot invoke an object which is possibly 'undefined'. ts(2722)"

Why? Function equals to what typed in {}, isn't it?

How can I correct it?

My question is probably so primitive that I didn't find anything.

1
  • Well, b.isIdZero is undefined at runtime. What do you expect to happen there (ignoring all the TypeScript types)? Commented Nov 3, 2021 at 16:56

2 Answers 2

2
isIdZero?():boolean{

The question mark here means you're defining isIdZero so it may be undefined, instead of being a function. Since it might be undefined, typescript won't let you call it unless you check it first. Eg:

if (b.isIdZero) {
  alert(b.isIdZero());
}

If you want isIdZero to be a mandatory property, then remove the question mark. However, this change will cause the following code to start showing a type error, since the object doesn't have all the properties it needs to be an A:

let b: A = {
    id:9
}

You can either add the function to the object you're creating

let b: A = {
  id: 9,
  isIdZero: () => false,
}

Or you can stop trying to call this an A, and just let it be an object with an id that's a number:

let b = {
  id: 9,
}
Sign up to request clarification or add additional context in comments.

5 Comments

The problem is when I added this check, my program told me, that there is no such property! It's undefined! Or so in my original code, not in this example.
What should I do now???
What's your goal? Do you want all A's to have an isIdZero function? Or for some of them to have it, and some not?
All of them should have this function, I added "?" because of some errors appeared whithout it. Originally it's function of validation of name and phone (I'm doing a phonebook)
K, start by making it mandatory by deleting the ?. In most cases, you'll then just call new A() everwhere you need an A, and it will just work, as in your first line let a : A= new A(0);. If you do need to manually create an object that resembles an A (like you're trying to do on the second line), then you will need to add all the properties to it yourself. That includes adding an isIdZero property to it.
0

the ? operator you are using after isIdZero? tells typescript that you expect a possibility of this property being undefined.

It doesn't seem like that's necessary, so you can remove that.

1 Comment

The problem is when I added this check, my program told me, that there is no such property! It's undefined! Or so in my original code, not in this example. What should I do now???

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.