我有一个方法:
public cancelOperation(OperationId: string): Promise<void>
{
// some calls
}我从另一种方法获得OperationId:
let operationId = GetOperationId() {}返回可空OperationId,
operationId?: string;因此,当传递从GetOperationId()接收到的GetOperationId时,我得到的错误是OperationId是可空的,因此不能在方法cancelOperation()中使用
那么,是否有任何C#等价于
operationId .Value 或
operationId .HasValue 方法在打字稿中,这样我就可以使用它。
尽管我们可以选择检查operationId,例如:
cancelOperation(playAudioResult.operationId ? playAudioResult.operationId : "")但是我不想用它。
我需要一个像这样的解决方案:cancelOperation(operationId.?)
发布于 2022-01-11 13:28:09
if (operationId)将在TypeScript中检查null。如果值不是true或undefined,它将返回undefined。
发布于 2022-01-11 13:52:21
您可以使用if来检查operationI是否为空。另一种方法是无效的聚结算子。
它是一个逻辑运算符,当其左侧操作数为null或未定义时,返回其右侧操作数。
它只在值不为空的情况下执行,
let x = operationI??NULL发布于 2022-01-14 09:56:56
以下是@ShamPooSham https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator共享的文章
正如本文所解释的,我找到了从可空属性中获取值的方法。
我们得把!属性之后,获取值(假设它不是空的)
// 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
}类似于我的方法,我可以使用:
cancelOperation(playAudioResult.operationId!) // notice the !https://stackoverflow.com/questions/70667597
复制相似问题