Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the question mark for in a Typescript parameter name

Tags:

typescript

People also ask

What does question mark after variable?

The question mark after the variable is called Optional chaining (?.) in JavaScript. The optional chaining operator provides a way to simplify accessing values through connected objects when it's possible that a reference or function may be undefined or null.

What is question mark after variable JavaScript?

The conditional or question mark operator, represented by a ? , is one of the most powerful features in JavaScript. The ? operator is used in conditional statements, and when paired with a : , can function as a compact alternative to if...else statements. But there is more to it than meets the eye.

What does two question marks mean in TypeScript?

Double question marks(??) or nullish coalescing operator helps us to assign default values to null or undefined variables in Angular and Typescript. It's often called as Null coalescing operator. It's introduced as part of Typescript 3.7 version.

What does a question mark mean in react?

“Question mark” or “conditional” operator in JavaScript is a ternary operator that has three operands. The expression consists of three operands: the condition, value if true and value if false.


It is to mark the parameter as optional.

  • TypeScript handbook https://www.typescriptlang.org/docs/handbook/2/functions.html#optional-parameters
  • TypeScript Deep Dive https://basarat.gitbook.io/typescript/type-system/functions#optional-parameters

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  
}

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

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


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.