0
votes

I've got a typescript method that returns a structured object:

export interface LoginResult{
  success:Boolean,
  message:String
}

login():LoginResult{
  ...
  // do login logic
  return {
    success: successVal,
    message: messageVal
  }
}

Rather than having to define the interface distinctly though I would like to be able to simply define the return type inline as part of the method call, something like the following:

login():LoginResult:{success:Boolean, message:String}{
  ...
  // do login logic
  return {
    success: successVal,
    message: messageVal
  }
}

Is this possible and if so what is the correct syntax?

1
You can just do login(): { success: boolean, message: string } { ... }, but you can't then name the type. - jonrsharpe
@jonrsharpe: That is an answer and should be posted as such -_- - H.B.
@Peter Nixey also pay attention to difference between boolean and Boolean typescriptlang.org/docs/handbook/declaration-files/… - Aleksey L.
thanks all - @jonrsharpe if you'd like to post as an answer I'll accept and Aleksey, good heads up - Peter Nixey

1 Answers

0
votes

Posting the answer from @jonrsharpe above:

You can just do login(): { success: boolean, message: string } { ... } but you can't then name the type