Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何告诉Typescript在本例中是一个函数';s的返回类型从不为null_Angular_Typescript_Strictnullchecks - Fatal编程技术网

Angular 如何告诉Typescript在本例中是一个函数';s的返回类型从不为null

Angular 如何告诉Typescript在本例中是一个函数';s的返回类型从不为null,angular,typescript,strictnullchecks,Angular,Typescript,Strictnullchecks,我试图让typescriptstrictNullChecks在Angular 5项目中工作 我有一张表格: this.signinForm = this.fb.group({ emailAddress: ['', NGValidators.isEmail()], password: ['', Validators.required], rememberMe: false, }); 我可以使用this.signinfo.get('rememberMe')获取rememberMe控件。

我试图让typescript
strictNullChecks
在Angular 5项目中工作

我有一张表格:

this.signinForm = this.fb.group({
  emailAddress: ['', NGValidators.isEmail()],
  password: ['', Validators.required],
  rememberMe: false,
});
我可以使用
this.signinfo.get('rememberMe')
获取
rememberMe
控件。然而,
FormGroup#get
方法返回的是
AbstractControl | null
,因此typescript不喜欢
this.signinfo.get('rememberMe').value
(因为它认为
this.signinfo.get('rememberMe')


是否可以告诉typescript,在本例中,
this.signinfo.get('rememberMe')
的返回总是
AbstractControl
,而不是
AbstractControl | null

我们可以通过将表达式包装为
作为[type]
来强制键入
typescript
。在您的情况下,它将是
(this.signinfo.get('rememberMe')作为AbstractControl)。value

我们可以通过将表达式包装为[type]来强制键入
TypeScript
。在您的情况下,它将是
(this.signinfo.get('rememberMe')作为抽象控件)。value

使用
操作员:

this.signinForm.get('rememberMe')!.value

使用
操作员:

this.signinForm.get('rememberMe')!.value

您可以始终将其包装为
作为any或作为AbstractControl
(this.signinfo.get('rememberMe')作为AbstractControl)。value
@Szarik真棒!那正是我要找的!如果您将其作为答案发布,我会将其标记为正确。您可以始终将其包装为
作为任意,或作为AbstractControl
作为
(this.signinfo.get('rememberMe')作为AbstractControl)。value
@Szarik真棒!那正是我要找的!如果你把它作为一个答案贴出来,我会把它标记为正确的。我决定把它作为正式答案,因为它能更好地解决问题。不过,这两个答案都很好/非常有帮助(而且,很明显,这两个答案都是问题的有效答案)。我决定将此作为正式答案,因为它为所问问题提供了更好的解决方案。然而,这两个答案都非常有用(显然,这两个答案都是问题的有效答案)。