1

I have referred other answers but could not solve my problem

console.log(`home.ts : ${result} : ${typeof result},${typeof this.points}`);

returns

home.ts : 50 : number,number

but this line

 this.points = result;

throws the following error:

Typescript Error: Type '{}' is not assignable to type 'number'.

full function

getdefaultscore(){
  this.authService.getdefaultscore().then(
    (result)=>{
      console.log(`home.ts : ${result} : ${typeof result},${typeof this.points}`);
      this.points = result;
    },
    (err)=>{
      this.authService.alertnointernetconnection();
    }
  )
}

this.points is defined as number.

definition of getdefaultscore()

  getdefaultscore(){
    return new Promise((resolve, reject)=>{
        let headers = new Headers();
        headers.append('Content-Type','application/json');
        this.http.post('mysite.com/ionuserpoint.php',JSON.stringify({defaultpoints:true}),headers)
        .subscribe(
            (res) =>{
                let data = res.json();
                data = parseFloat(data);
                alert(`defaults points : ${data}`);
                resolve(data);
            },
            (err) =>{
              reject(err);
            }
        );
    });
  }

I remove http://www before mysite.com because stackoverflow throws some error.

9
  • 2
    And what is a definition of getdefaultscore() ? Commented Sep 6, 2017 at 8:47
  • How do you declare this.points? points: number ? Commented Sep 6, 2017 at 8:49
  • are you using points: number; or points = number Commented Sep 6, 2017 at 8:49
  • @Fetra R yes bro Commented Sep 6, 2017 at 9:05
  • 1
    @vSugumar I see the results at runtime. But what is a definition of return type of this.authService.getdefaultscore()? It must be Promise<OfSomething> I believe Commented Sep 6, 2017 at 9:10

1 Answer 1

2

Th problem is in getdefaultscore which returns a Promise<{}> ideally you should change it to return a Promise<number>. Since you do not provide the code for getdefaultscore it is difficult to say what change you should make there. To solve the problem on the caller side you can manually type the result :

declare function getdefaultscore(): Promise<{}>; // Dummy declaration 
var points : number;
function x() {
  getdefaultscore().then((result: number)=>{
    points = result;
  },
  (err)=>{
  })
}

Another version you can consider in Typescript is using async/await to simplify your Promise code:

async function x2() {
  try {
    points = <number>await getdefaultscore()
  } catch (ex) {

  }
}

Edit: You added the getdefaultscore after the original answer. Since you create the promis manually, you just need to type it to Promise<number>.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried parseFloat(resolve) but it throwed a different error
Edited the answer, you just need to type the promise to number
That is assuming the returned json is just a single number, but that is a different problem, if data is a more complex object pareseFloat will fail in other ways

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.