Home > Back-end >  What is HasValue and .Value equivalent in TypeScript
What is HasValue and .Value equivalent in TypeScript

Time:01-11

I have a method :

public cancelOperation(OperationId: string): Promise<void> 
 {
         // some calls
 }

I get OperationIdfrom another method :

let operationId = GetOperationId() {}

which return nullable OperationId,

 operationId?: string;

so when passing the OperationId received from GetOperationId(), I get error that operationId is nullable, so cant be used in method cancelOperation()

So is there any C# equivalent of

 operationId .Value 

or

operationId .HasValue 

method in Typescript so I could go for it.

Though we have option to check operationId like :

 cancelOperation(playAudioResult.operationId ? playAudioResult.operationId : "")

but I dont want to use it.

I need a solution which will something like : cancelOperation(operationId.?????)

CodePudding user response:

if (operationId) will check for null in TypeScript. It will return true if the value is not null or undefined.

CodePudding user response:

if(variable == null) {
     console.log('The variable is undefined or null');
   }

this is a way

CodePudding user response:

You can use if to check whether the operationI is null or not. Tho other way is nullish coalescing operator.

It is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined.

It carries out only if the value is not null like,

let x = operationI??NULL
  •  Tags:  
  • Related