简体   繁体   中英

Angular2/TypeScript: error TS2345: Argument of type 'String' is not assignable to parameter of type 'string'

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:

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:

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?

private searchTerms = new Subject<String>();

change it to

private searchTerms = new Subject<string>();

String is a class and string is a datatype .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM