简体   繁体   English

如何分配字符串| 在 TypeScript 中未定义为字符串?

[英]How to assign string | undefined to string in TypeScript?

I want to assign a variable, which is string |我想分配一个变量,它是字符串 | undefined, to a string variable, as you see here:未定义,到字符串变量,如您在此处看到的:

private selectedSerialForReplace(): string | undefined {
    return this.selectedSerials.pop();
  }

luminaireReplaceLuminaire(params: {  "serial": string; "newserial": string; }, options?: any): FetchArgs {
............
}

luminaireReplaceLuminaire({serial: this.selectedSerialForReplace(), newserial: response.output});

I get this error:我收到此错误:

Argument of type '{ serial: string | '{ serial: string | 类型的参数undefined;未定义; newserial: any;新闻:任何; }' is not assignable to parameter of type '{ "serial": string; }' 不能分配给 '{ "serial": string; 类型的参数。 "newserial": string; “新闻”:字符串; }' }'

I cannot change selectedSerialForReplace() function to return anything else.我无法更改 selectedSerialForReplace() 函数以返回任何其他内容。 Could you please help me?你能帮帮我吗?

The typescript compiler performs strict null checks, which means you can't pass a string | undefined typescript 编译器执行严格的空检查,这意味着您不能传递string | undefined string | undefined variable into a method that expects a string . string | undefined变量转换为需要string的方法。

To fix this you have to perform an explicit check for undefined before calling luminaireReplaceLuminaire() .要解决此问题,您必须在调用luminaireReplaceLuminaire()之前对undefined执行显式检查。

In your example:在你的例子中:

private selectedSerialForReplace(): string | undefined {
    return this.selectedSerials.pop();
}

luminaireReplaceLuminaire(params: {  "serial": string; "newserial": string; }, options?: any): FetchArgs {
    ............
}

const serial = this.selectedSerialForReplace();
if(serial !== undefined) {
    luminaireReplaceLuminaire({serial, newserial: response.output});
}

If you are sure that serial could not be undefined you can use the !如果您确定序列不能undefined ,则可以使用! post-fix operator 后置运算符

luminaireReplaceLuminaire({serial: this.selectedSerialForReplace()!, newserial: response.output});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 typescript 类型错误不能分配字符串或未定义给字符串[] - typescript type error cannot assign string or undefined to string[] 如何将字符串或空值分配给变量 TypeScript - How to assign either a string or null to a variable TypeScript 如何在TypeScript中将箭头函数返回值分配给string [] - How to assign arrow function return value to string[] in TypeScript 如果值在 typescript 数组中,则分配新的字符串值 - Assign new string value if value is in typescript array 对象可能在 Typescript 中的字符串值“未定义” - Object is possibly 'undefined' in Typescript on string value '' 'string | 类型的参数 undefined' 不能分配给'string' 类型的参数。 typescript '严格' - Argument of type 'string | undefined' is not assignable to parameter of type 'string'. typescript 'strict' 输入'字符串| null' 不能分配给类型 'string | TypeScript 的未定义错误 - Type 'string | null' is not assignable to type 'string | undefined error with TypeScript 'string | 类型的参数 undefined' 不可分配给“string”类型的参数 - TypeScript 错误 - Argument of type 'string | undefined' is not assignable to parameter of type 'string' - TypeScript error Typescript:类型参数'string | undefined' 不能分配给“string”类型的参数 - Typescript: Argument of type 'string | undefined' is not assignable to parameter of type 'string' 为什么 typescript 将我的值设置为“未定义”字符串而不是未定义? - Why is typescript setting my value to a 'undefined' string instead of an undefined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM