1

As title. I know that a question mark in object fields means that it's not required in this type. But I don't know what it means when it's used in defining a class, to be specific I saw this in the source code:

export class MockServer {
  private server?: Server;
...

Could anyone explain why we need it here? Does it mean the class doesn't have this property to be this type? But this explanation seems to be strange.

NeoZoom.lua
  • 2,269
  • 4
  • 30
  • 64
  • Yes, they are [optional class properties](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#optional-class-properties). – jcalz Jan 19 '22 at 03:10
  • @jcalz: Could you share how did you find it? I didn't really want to waste you expert's time but I use some comb. of the words in my title to Google and found nothing. – NeoZoom.lua Jan 19 '22 at 03:11
  • 1
    I have a bunch of the TypeScript handbook in my browser history; in this case I searched for `site:stackoverflow.com typescript class property "question mark"` and `site:stackoverflow.com typescript class property optional` in Google and found various results here, including [this question](https://stackoverflow.com/questions/18916104/optional-class-members-in-typescript). – jcalz Jan 19 '22 at 03:18
  • @jcalz: Thanks! I actually did the same thing learning python in the past. All heros endure pains. – NeoZoom.lua Jan 19 '22 at 03:22

0 Answers0