A proof-of-concept project exploring interop between NestJS, TypeScript and KotlinJS.
Demonstrates a Kotlin-first code sharing mechanism, where data models and business logic can be defined in Kotlin and utilized in Nest.
Interaction with TypeScript files from Kotlin is not currently supported and as a result, neither is definition of Nest modules & controllers in Kotlin.
package foo
fun getFoo():String {
return "Foo!"
}
import * as interop from "interop"
@Controller()
export class FooController {
constructor() {}
@Get('foo')
async get(@Request() req, @Response() res, next) {
res.status(HttpStatus.OK).send(interop.foo.getFoo());
}
}
- Interoperation is achieved by targeting JS when compiling Kotlin, and then generating TypeScript definitions for the JS output.
- The project can be built and run with
npm start
andnpm run start:prod
similar to the other starter repos. - Kotlin is compiled to JS via Gradle and the included Gradle wrapper. KotlinJS configuration can be seen in
build.gradle
. - The NPM scripts which define the build process can be viewed in
package.json
.
- Compiles Kotlin to JS by calling
./gradlew assemble
, outputting a CommonJS module that's ready to use with Node - Calls
dtsmake
to generate typings from the JS output, complete with realistic types thanks to type inference - Uses
shx
to copy the JS output tonpm_modules
so it can be imported by Node - this could use a second look. - Calls
tsc
- Runs with
node index.js
ornode dist/server.js
In addition to NPM, the JRE/JDK will also be required to run the gradle build and compile the Kotlin code. Use of IntelliJ IDEA is highly recommended for working with Kotlin.
Probably not.