why I can't create a property dynamically?

why can i not
why can't i meaning
why can't i see
why i can't download
why can't i see anything
why can't i liz phair movie
can or can't
can't see meaning

I'm new to typescript, just a question on. We know that we can define a property in javascript dynamically as:

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

but we can't do the same thing when use typescript as:

class Rectangle {
  constructor(height:number, width:number) {
    this.height = height;   //error 
    this.width = width;     //error 
  }
}

I know that if add access identifier such as pubic as prefix of parameters in the constructor as:

 ...
 constructor(public height:number, public width:number) {...}  //which create declaration automatically

will fix the problem, but I am just wondering, isn't that typescript the superset of javascript? so typescript should support all valid javascript syntax as well?

Following is the flow of a TS interpretation.

  1. All properties, methods are initialized.
  2. Then during instantiation, constructor is called and above properties are set with default values.

In your code:

class Rectangle {
  constructor(height:number, width:number) {
    this.height = height;   //error 
    this.width = width;     //error 
  }
}

There are no properties mentioned and hence you get an error

Why I Can't.. or Why Can't I?, Our beliefs and thoughts impact our lives more than any relationship.It would be wise to evaluate how we came to them and whether they truly� As that anti-masker having a tantrum in a Panera pointed out, you can smell a fart even though it passes through the fabric of your pants. Her point seems to be that fabric cannot block air, and

It is a strict syntactical superset of JavaScript, and adds optional static typing to the language

Another thing to note is that things like features which are not strongly-typed or similar things won't work in TypeScript, this phenomena is similar to it.

Liz Phair - Why Can't I? (Official Video), Music video by Liz Phair performing Why Can't I?. #LizPhair #WhyCantI #Vevo.Duration: 3:40 Posted: Feb 24, 2009 The coronavirus pandemic has delivered a shock to tens of millions of people in rich countries around the world: Suddenly, they can't buy the food they want, when they want.

You can do it, but of course you'll fight against the type-checking feature of the language.

class Rectangle {
  constructor(height:number, width:number) {
    (this as any).height = height;
    (this as any).width = width;
  }
}

const rt = new Rectangle(100, 200);
console.log((rt as any).width);

That is, if you cast to any, you can do everything as you would do in plain JavaScript.

Insomnia, Causes of insomnia: Figuring out why you can't sleep. In order to properly treat and cure your insomnia, you need to become a sleep detective. Emotional issues � Note that you cannot repair a damaged PDF itself. However, if you have the source file, you can re-create the PDF using an Adobe product, such as Acrobat or Photoshop Elements. Most modern web browsers can easily open PDFs. If you’re having trouble opening a specific PDF on a web page, try the

I Can't Take a Deep Breath: Causes, Treatments, and Outlook, Have you ever felt like you couldn't take a deep breath? Doctor's refer to this symptom as dyspnea. It can be a symptom of many health conditions, including� Problem: You're unable to select and change the text in the footer at the bottom of your slides. Solution: If the text you want to edit is truly in a footer, you should be able to edit it by going to the Insert tab and then clicking Header & Footer.

I can't log in to Facebook. | Facebook Help Center, You can search for your account by name, email address, or phone number. Follow the on-screen steps to reset the password for your account. Recover your � I can’t reset my Facebook password because I can't access the email or mobile phone number on my account. Here are some things you can try to get back into your account so you can reset your password.

Can't sign in to your Google Account, If you can't sign in to your Google Account in Gmail, Google Drive, Google Play, or elsewhere, select the issue that most closely applies to you. Follow the� If you can't back up your iPhone, iPad, or iPod touch to a computer, or if you can't restore your device from a computer backup, learn what to do. Try again after each step Make sure your Mac or PC is up-to-date.

Comments
  • what you got is NOT a syntax error, it's a type error. So yeah it does support all valid js syntax. Yet, if you don't type annotate your stuff properly, it'll give you type error even when the syntax is right. That what typescript does on top of javascript.
  • @Rajesh thanks for the answer, but when you says All properties, methods are initialized in step 1, let's take height property for example and the corresponding passing argument valye is 3, does it mean sth like :this.height is first set to 0 (default value first) then constructor set this.height to 3?
  • @secondimage sort of. What I said about step 1, is the fact that classes are initialized at compile time and objects at run time. So when your code is being compiled, TS finds no properties. So when you do this.<something>, there is no reference to this property in associated type. Hence error
  • Using as any in TS is a bad convention
  • I know, but in practice this is what the user asked.
  • As a senior developer / member of community you should not suggest this kind of solutions
  • @MarioVernari as more experienced, if OP or anyone is asking a patch, its your responsibility to point it out and suggest the right way. We are here to share knowledge and not do someone's work. By sharing right practices, you'll help readers more than you know
  • @Rajesh I don't know why the user asked a similar question, but obviously it sounds as a "nonsense" subject: if you choose TypeScript is because you want to leverage the type-checking, where the plain JS is much shooting at your own feet. IMHO, there are situations where you have no other ways than cast as any, basically when a library offers no mapping facility. I myself would never do that, but I think it's also importanto to know how the things are.