Cannot generate a working express header with ts2kt


#1

Hello,
so this is the d.ts:

// Type definitions for Express 4.x
// Project: http://expressjs.com
// Definitions by: Boris Yankov <https://github.com/borisyankov>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
/* =================== USAGE ===================
    import * as express from "express";
    var app = express();
 =============================================== */

/// <reference types="express-serve-static-core" />
/// <reference types="serve-static" />
import * as bodyParser from "body-parser";
import * as serveStatic from "serve-static";
import * as core from "express-serve-static-core";

/**
 * Creates an Express application. The express() function is a top-level function exported by the express module.
 */
declare function e(): core.Express;

declare namespace e {
    /**
     * This is a built-in middleware function in Express. It parses incoming requests with JSON payloads and is based on body-parser.
     * @since 4.16.0
     */
    var json: typeof bodyParser.json;

    /**
     * This is a built-in middleware function in Express. It serves static files and is based on serve-static.
     */
    var static: typeof serveStatic;

    /**
     * This is a built-in middleware function in Express. It parses incoming requests with urlencoded payloads and is based on body-parser.
     * @since 4.16.0
     */
    var urlencoded: typeof bodyParser.urlencoded;

    export function Router(options?: RouterOptions): core.Router;

    interface RouterOptions {
        /**
         * Enable case sensitivity.
         */
        caseSensitive?: boolean;

        /**
         * Preserve the req.params values from the parent router.
         * If the parent and the child have conflicting param names, the child’s value take precedence.
         *
         * @default false
         * @since 4.5.0
         */
        mergeParams?: boolean;

        /**
         * Enable strict routing.
         */
        strict?: boolean;
    }

    interface Application extends core.Application { }
    interface CookieOptions extends core.CookieOptions { }
    interface Errback extends core.Errback { }
    interface ErrorRequestHandler extends core.ErrorRequestHandler { }
    interface Express extends core.Express { }
    interface Handler extends core.Handler { }
    interface IRoute extends core.IRoute { }
    interface IRouter<T> extends core.IRouter { }
    interface IRouterHandler<T> extends core.IRouterHandler<T> { }
    interface IRouterMatcher<T> extends core.IRouterMatcher<T> { }
    interface MediaType extends core.MediaType { }
    interface NextFunction extends core.NextFunction { }
    interface Request extends core.Request { }
    interface RequestHandler extends core.RequestHandler { }
    interface RequestParamHandler extends core.RequestParamHandler { }
    export interface Response extends core.Response { }
    interface Router extends core.Router { }
    interface Send extends core.Send { }
}

export = e;

and this is the outcome after running ts2kt -d header express.d.ts:

  • express.kt
    @file:Suppress("INTERFACE_WITH_SUPERCLASS", "OVERRIDING_FINAL_MEMBER", "RETURN_TYPE_MISMATCH_ON_OVERRIDE", "CONFLICTING_OVERLOADS")

    import kotlin.js.*
    import kotlin.js.Json
    import org.khronos.webgl.*
    import org.w3c.dom.*
    import org.w3c.dom.events.*
    import org.w3c.dom.parsing.*
    import org.w3c.dom.svg.*
    import org.w3c.dom.url.*
    import org.w3c.fetch.*
    import org.w3c.files.*
    import org.w3c.notifications.*
    import org.w3c.performance.*
    import org.w3c.workers.*
    import org.w3c.xhr.*

    @JsModule("e")
    external fun e(): core.Express = definedExternally
  • express.e.kt
    @file:Suppress("INTERFACE_WITH_SUPERCLASS", "OVERRIDING_FINAL_MEMBER", "RETURN_TYPE_MISMATCH_ON_OVERRIDE", "CONFLICTING_OVERLOADS")
    @file:[JsQualifier("e") JsModule("e")]
    package e

    import kotlin.js.*
    import kotlin.js.Json
    import org.khronos.webgl.*
    import org.w3c.dom.*
    import org.w3c.dom.events.*
    import org.w3c.dom.parsing.*
    import org.w3c.dom.svg.*
    import org.w3c.dom.url.*
    import org.w3c.fetch.*
    import org.w3c.files.*
    import org.w3c.notifications.*
    import org.w3c.performance.*
    import org.w3c.workers.*
    import org.w3c.xhr.*

    external var core: dynamic = definedExternally
    external var json: dynamic /* "TypeQuery" kind unsupported yet here! (types/express.d.ts:27:14 to 27:37) */ = definedExternally
    external var static: dynamic /* "TypeQuery" kind unsupported yet here! (types/express.d.ts:32:16 to 32:35) */ = definedExternally
    external var urlencoded: dynamic /* "TypeQuery" kind unsupported yet here! (types/express.d.ts:38:20 to 38:49) */ = definedExternally
    external fun Router(options: RouterOptions? = definedExternally /* null */): core.Router = definedExternally
    external interface RouterOptions {
        var caseSensitive: Boolean? get() = definedExternally; set(value) = definedExternally
        var mergeParams: Boolean? get() = definedExternally; set(value) = definedExternally
        var strict: Boolean? get() = definedExternally; set(value) = definedExternally
    }
    external interface Application : core.Application
    external interface CookieOptions : core.CookieOptions
    external interface Errback : core.Errback
    external interface ErrorRequestHandler : core.ErrorRequestHandler
    external interface Express : core.Express
    external interface Handler : core.Handler
    external interface IRoute : core.IRoute
    external interface IRouter<T> : core.IRouter
    external interface IRouterHandler<T> : core.IRouterHandler<T>
    external interface IRouterMatcher<T> : core.IRouterMatcher<T>
    external interface MediaType : core.MediaType
    external interface NextFunction : core.NextFunction
    external interface Request : core.Request
    external interface RequestHandler : core.RequestHandler
    external interface RequestParamHandler : core.RequestParamHandler
    external interface Response : core.Response
    external interface Router : core.Router
    external interface Send : core.Send

It’s failing because there is no reference to core variable.
I tried to do something like external var core: dynamic = definedExternally but it didn’t work.

Can you help me?

PS: it would be really useful to have a directory of working headers.


#2

The core is the imported name of express-serve-static-core, so you have to convert this library too and maybe fix manually cross-references.

PS: it would be really useful to have a directory of working headers.

We are working on the infrastructure for that and I hope it will be available soon for contributions.


#3

Oh I see! Well, that is going to be a nightmare for a npm with lots of depedendencies.