Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error TS2314: Generic type 'Promise<T>' requires 1 type argument(s)

Tags:

I have used Promise and observables logic to fetch data from server using "get". It was working till yesterday. SUddenly it starts throwing the above error. Please help me finding the error. I am getting "Generic type 'Promise' requires 1 type argument(s)" error.

@Injectable()
export class myBlogService{

  // Property to hold root server URL i.e host
  private serverUrl:string = "app/data.json"

  constructor(private http:Http) {}

  // check function in service to check control is coming to service
  check(){
    alert("getting clicked from service");
  }

  // get function to get data from server
  // basically blog datas
  get(): Promise {
    return this.http.get(this.serverUrl)
               .map(response => response.json())
  }
}


/**
 * 
 * My Components
 * 
 */
@Component({
  selector: 'my-app',
  providers: [myBlogService],
  styleUrls: ['app/css/app.css'],
  template: `
    <h1 (click)= check()>My First Angular 2 App</h1>
    <button (click)=get()>Get My Name</button>
    <h1>{{getResponse.name}}</h1>
  `
})
export class myBlogApp {

  // Property to hold blog data
  public getResponse = {"name": "", "age": ""};

  constructor(protected myblogservice:myBlogService){}

  // check function to check control is going to service
  check() {
    this.myblogservice.check();
  }

  // get function calls service get function which return data from server
  get(){
    this.myblogservice.get().subscribe(data => {
      this.getResponse = data;
    });
  }
}


/**
 * 
 * NgModule Declaration
 * 
 */
@NgModule({
  imports:      [ BrowserModule, HttpModule ],
  declarations: [ myBlogApp ],
  providers:    [ ],
  bootstrap:    [ myBlogApp ]
})
export class app{}


/**
 * 
 * App engine entry point
 * 
 */
const platform = platformBrowserDynamic();
platform.bootstrapModule(app);

When "promise: " is given, still it gives issue like "error TS2339: Property 'subscribe' does not exist on type 'Promise'".

I tried different solution but no luck yet.

like image 414
Jyotirmay Avatar asked Sep 29 '16 23:09

Jyotirmay


Video Answer


2 Answers

You need to add the specific type.

If it contains no data and is being used purely for the resolve/reject functionality, use:

Promise<void>

Ultimately this is a type signature like any other, so you can use:

Promise<any> 

https://basarat.gitbooks.io/typescript/content/docs/promise.html

like image 93
Tabares Avatar answered Sep 20 '22 09:09

Tabares


Instead of using Promise try to use Observable, replace:

get(): Promise {
  return this.http.get(this.serverUrl)
               .map(response => response.json())
}

with

get(): Observable<any> {
  return this.http.get(this.serverUrl)
               .map(response => response.json())
}
like image 28
ulou Avatar answered Sep 22 '22 09:09

ulou