Antoine Doeraene
2 min readJul 24, 2020

--

Hello Geoffroy, thanks for the kind word!

Regarding your question, it's a very interesting one! The overall answer being: definitely, yes.

However, there could be two different ways of doing that.

- the first, easiest way to set up would be to simply to make classes that you export as above, and creating (in Angular) components extending that class (whose body is then (mostly) empty and where the constructor simply calls the constructor of the parent class). The big advantage of that is that you keep TypeScript and "genuine" Angular around if need be. For example, you would have in Scala

@JSExportTopLevel("HomeComponent")

@JSExportAll

final class HomeComponent(httpClient: HttpClient) {

private val clickSubject = new Subject[true]

val values =

clickSubject.pipe(flatMap((_, _) => httpClient.get("api/stuff")))

def click(): Unit = clickSubject.next(true)

}

and then in TypeScript

export class FromScalaComponent extends HomeComponent implements OnInit {

constructor(httpClient: HttpClient) {

super(httpClient);

}

ngOnInit() {}

}

That way, you can use the `values` observable and the `click` handler inside your component html

- the other approach would be to completely get rid of TypeScript (this has been done in a PoC here https://github.com/ScalablyTyped/Demos/tree/master/angular ). Oddly enough, given the previous bullet point, the big advantage is that you completely get rid of TypeScript. The disadvantage is that there will be a cost to pay upfront: find a nice way to handle the `.component.html` and `.component.css` files, because Angular is doing quite a lot of shenanigans there. It is doable, but need some thinking (for example using instead libraries like scala-tags)

The question is: when you would want to do that? And there I see two possible answers:

- you mostly don't need TypeScript anymore which means that if the backend is in Scala, there is only one language to learn for your developers.

- you will be able to fully use what the Scala compiler has to offer. A good example of that would be automatic derivation of `FormGroup`s, for example. In Scala, it would be possible to code something (advanced but not long) that would enable you to do something like:

case class Address(street: String, postalCode: Int)

case class Person(name: String, address: Address)

and then simply

val fg = new FormGroup[Person]

and the Scala compiler would be able to generate, at compile time and in a completely type safe manner, an Angular FormGroup representing instances of Persons to be used inside your component.

I hope that answer your question.

--

--

Antoine Doeraene
Antoine Doeraene

Written by Antoine Doeraene

Mathematician, Scala enthusiast, father of two.

No responses yet