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

19,481

Solution 1

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

Solution 2

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())
}
Share:
19,481

Related videos on Youtube

Jyotirmay
Author by

Jyotirmay

:):

Updated on June 04, 2022

Comments

  • Jyotirmay
    Jyotirmay over 1 year

    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.

  • Victor Zamanian
    Victor Zamanian about 5 years
    Nowhere in that link can I find exactly what the type should be in the <> angle brackets of Promise<>. I can only infer that it should be the same type as the value of the resolve() case. Is there any good documentation of what I should put as the type of the generic Promise in TypeScript?