Angular2/TypeScript:错误 TS2345:“字符串”类型的参数不可分配给“字符串”类型的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40203870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular2/TypeScript: error TS2345: Argument of type 'String' is not assignable to parameter of type 'string'
提问by micronyks
I have the following code, which I am using to create a Casino Management System:
我有以下代码,用于创建赌场管理系统:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import { SearchService } from './search.service';
import { Data } from './datatypings';
@Component({
moduleId: module.id,
selector: 'casino-search',
templateUrl: 'search.component.html',
styleUrls: [ 'search.component.css' ],
providers: [SearchService]
})
export class SearchComponent implements OnInit {
data: Observable<Data[]>;
private searchTerms = new Subject<String>();
constructor(
private searchService: SearchService,
private router: Router) {}
// Push a search term into the observable stream
search(term: string): void {
this.searchTerms.next(term);
}
ngOnInit(): void {
this.data = this.searchTerms
.debounceTime(150) // wait for 150ms pause in events
.distinctUntilChanged() // ignore if next search term is same as previous
.switchMap(term => term // switch to new observable each time
// return the http search observable
? this.searchService.search(term)
// or the observable of empty data if no search term
: Observable.of<Data[]>([]))
.catch(error => {
// TODO: real error handling
console.log(error);
return Observable.of<Data[]>([]);
});
}
gotoDetail(data: Data): void {
let link = ['/detail', data.id];
this.router.navigate(link);
}
}
However, when I attempt to transpile this code from TypeScript to JavaScript I get this error:
但是,当我尝试将此代码从 TypeScript 转换为 JavaScript 时,出现此错误:
app/data/search.component.ts(37,37): error TS2345: Argument of type 'String' is not assignable to parameter of type 'string'.
'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.
Now, I've fixed the error simply by changing String
to string
within the angle braces but I want to be able to proceed in future without creating similar errors, and also to write the best code I can... So I'm wondering if someone could clear this up for me:
现在,我只是通过在尖括号内更改String
为来修复错误,string
但我希望将来能够继续进行而不会产生类似的错误,并且还编写了我能写的最好的代码......所以我想知道是否有人可以帮我解决这个问题:
I think I understand that a parameter with a wrapper class is something which encapsulates a primitive class and provides some sort of additional, usually interfacing related, behaviour (correct me if I'm wrong here), and I see why it might then not be possible to assign the contents of what is essentially a broader declared variable type to the more simple primitive, there is another aspect of the error message which interests me, that is:
我想我明白带有包装类的参数是封装原始类并提供某种额外的、通常与接口相关的行为的东西(如果我在这里错了,请纠正我),我明白为什么它可能不是可以将本质上是更广泛声明的变量类型的内容分配给更简单的原语,但错误消息的另一个方面引起了我的兴趣,即:
Why would it be preferable to use type 'string' over type 'String' in Angular2/Typescript in such a general sense as the error seems to imply? What is it which makes this preferential?
为什么在错误似乎暗示的一般意义上,在 Angular2/Typescript 中使用“字符串”类型而不是“字符串”类型会更可取?是什么使这种优惠?
回答by micronyks
private searchTerms = new Subject<String>();
change it to
将其更改为
private searchTerms = new Subject<string>();
String
is a class
and string
is a datatype
.
String
是一个class
并且string
是一个datatype
。