0

I have a method :

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

I get OperationId from 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.?????)

Rajdeep
  • 788
  • 7
  • 26
  • `let operationId = GetOperationId() {}` doesn't look like correct syntax. – ShamPooSham Jan 11 '22 at 13:57
  • @ShamPooSham ok I have changed it – Rajdeep Jan 11 '22 at 14:04
  • You didn't change the line I was talking about – ShamPooSham Jan 11 '22 at 14:05
  • But if you mean that the type of `operationId` is `string | undefined`, typescript will be smart to remove the `| undefined` from the type if you do the call to `cancelOperation` within an `if(operationId)` – ShamPooSham Jan 11 '22 at 14:18
  • @ShamPooSham This is good but we are looking for .Value equivalent – Rajdeep Jan 11 '22 at 14:26
  • 1
    There is none because typescript and C# work in very different ways. But I would argue that this does practically the same thing: If the value is defined, it will be used. You can also use the not-null assertion operator (`!`) https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator – ShamPooSham Jan 11 '22 at 14:46
  • It's important to understand that the types in typescript are just static, they don't have any effect on the produced javascript, they're just a help to catch errors before compiling. So something which is of type `string | undefined` will not have a `.Value` property or anything of the kind, because `string | undefined` is not an object type. – ShamPooSham Jan 11 '22 at 14:48
  • I would recommend that you use `if(nullableVariable != null) { /* do things with nullableVariable */ } else { /* if you want it to behave like C#'s ".Value", you should throw here */ }` – ShamPooSham Jan 11 '22 at 14:51
  • In general, when switching languages, you shouldn't ask yourself what the equivalent of some other language is, you should ask yourself how it's supposed to be done in that language. All languages have different features that can't always be directly comparable. – ShamPooSham Jan 11 '22 at 14:54
  • It should be `true` or `false` for `null` and nullish values like `undefined`? There are no similar, so you surely should use operators or just pass variable directly for casting into boolean: `if (variable) {...}` – Xeelley Jan 11 '22 at 15:27

3 Answers3

0

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

klekmek
  • 533
  • 3
  • 11
0

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
Girija
  • 118
  • 3
0

Following the article shared by @ShamPooSham https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator

I got a way to get value from the nullable property as explained in the article.

We need to put ! after property and get the value(assuming that its not null)

// Compiled with --strictNullChecks
function validateEntity(e?: Entity) {
  // Throw exception if e is null or invalid entity
}
function processEntity(e?: Entity) {
 validateEntity(e);
 let s = e!.name; // Assert that e is non-null and access name
}

Similary in my method I can use :

cancelOperation(playAudioResult.operationId!)  // notice the !
Rajdeep
  • 788
  • 7
  • 26