324
export class Thread {
  id: string;
  lastMessage: Message;
  name: string;
  avatarSrc: string;

  constructor(id?: string,
              name?: string,
              avatarSrc?: string) {
    this.id = id || uuid();
    this.name = name;
    this.avatarSrc = avatarSrc;
  }
}

In id? what's the ? for?

5 Answers 5

346

It is to mark the parameter as optional.

7
  • 9
    I have seen $ symbol at the end of variable names. What does that mean?
    – Sunil Garg
    Feb 17, 2017 at 10:02
  • 21
    It is not related to TypeScript. I have seen such syntax in RxJs projects. From the doc: it is a "common RxJS convention to identify variables that reference a stream." github.com/redux-observable/redux-observable/blob/master/docs/… Feb 17, 2017 at 10:10
  • 9
    @SunilGarg $ postfix is usually a naming convention to mean the variable is an observable. Jul 2, 2019 at 19:53
  • 1
    What if the question mark is used in class properties? the links didn't resolve this. Jan 19, 2022 at 2:53
  • 1
    @NeoZoom.lua For class properties, the behavior is analogous to function parameters – declaring foo?: string; means that "foo" is optional, and the type of instance.foo becomes string | undefined.
    – Jo Liss
    Mar 25 at 18:41
67

parameter?: type is a shorthand for parameter: type | undefined

So what is the difference? Question mark means "optional".
More precisely parameter?: type is equal to parameter: type | undefined = undefined

4
  • 38
    Not exactly. Question mark means "optional". So it's a shorthand for "parameter: type | undefined = undefined"..
    – mcoolive
    May 7, 2020 at 17:50
  • 11
    Actually, it does not default to undefined, but rather to nonexistence. Masih's answer is correct: parameter?: type is a shorthand for parameter: type | undefined Note how differently = undefined behaves in this example.
    – lodewykk
    Sep 16, 2021 at 15:22
  • 19
    @lodewykk It's not exactly a shorthand, either. param?: type declares the parameter as optional, param: type | undefined doesn't. Example: const foo: { a?: string } = {} works but const foo: { a: string | undefined } = {} fails. Meanwhile, in function declarations @mcoolive is right and arg?: type is equivalent to arg: type | undefined = undefined, compare stackoverflow.com/questions/37632760/…
    – balu
    Sep 27, 2021 at 17:50
  • This is answer is not correct. The ? designator (not sure what it technically is classified as) is not shorthand for type | undefined. It's true that optional parameters will have a value of undefined at run time if they are not specified but as @balu pointed out type | undefined requires you to actually specify a value of that type or undefined or you'll get a compiler error. The optional ? thingy does not require you to specify a value.
    – d512
    Jan 27, 2023 at 22:26
47

This is to make the variable of Optional type. Otherwise declared variables shows "undefined" if this variable is not used.

export interface ISearchResult {  
  title: string;  
  listTitle:string;
  entityName?: string,
  lookupName?:string,
  lookupId?:string  
}
5
  • 5
    I do not agree that it indicates a "nullable" type. It indicates optional, not nullable. It is still valid, for instance, for title in the example above to have a value of null but it would be invalid for a class that claims to implement ISearchResult to be missing an entityName property at compile time. Oct 2, 2017 at 15:44
  • 19
    I think the correct name is "optional parameter". Nullable type would be string?. To have an optional nullable, you'd do name?: string?.
    – user276648
    Oct 26, 2017 at 13:42
  • @DvG Would you mind to improve your answer considering the comments by Josh Gallagher and user276648? Sep 14, 2018 at 9:19
  • @user1460043.. I have updated my answer. Thanks for notifying me
    – DvG
    Sep 14, 2018 at 9:22
  • I assume the phrasing "optional type" originates from languages like c++, scala, or python. Where you have a generic Optional<T> type. Actually, it would make more sense to put the ? after the type specification instead of after the variable name. If you don't pass anything to lookupId, then it will not have type string.
    – Marti Nito
    Dec 16, 2019 at 14:31
10

The ? in the parameters is to denote an optional parameter. The Typescript compiler does not require this parameter to be filled in. See the code example below for more details:

// baz: number | undefined means: the second argument baz can be a number or undefined

// = undefined, is default parameter syntax, 
// if the parameter is not filled in it will default to undefined

// Although default JS behaviour is to set every non filled in argument to undefined 
// we need this default argument so that the typescript compiler
// doesn't require the second argument to be filled in
function fn1 (bar: string, baz: number | undefined = undefined) {
    // do stuff
}

// All the above code can be simplified using the ? operator after the parameter
// In other words fn1 and fn2 are equivalent in behaviour
function fn2 (bar: string, baz?: number) {
    // do stuff
}



fn2('foo', 3); // works
fn2('foo'); // works

fn2();
// Compile time error: Expected 1-2 arguments, but got 0
// An argument for 'bar' was not provided.


fn1('foo', 3); // works
fn1('foo'); // works

fn1();
// Compile time error: Expected 1-2 arguments, but got 0
// An argument for 'bar' was not provided.
2

As of TypeScript 4.7, this is how the compiler would treat these scenarios:

interface OptTest {
  test: string
  test2: string
  test3?: string
  test4?: string | null
  test5: string | null
  test6: null
}

const testVar: OptTest = {} 
// Type '{}' is missing the following 
// properties from type 'OptTest': test, test2, test5, test6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.