How to export a type alias for an existing class

1k views Asked by At

I have a TS module that is deeply nested in namespaces. I can't avoid this because it is auto-generated from a java library (I am writing nativescript).

I want to "re-export" these nested classes in my own module, at the top level.

Say I have a class that is declared as such in native-types.d.ts:

declare module com {
    export module esp32 {
        export module blufi {
            export class BlufiClient {
                ....
            }
        }
    }
}

The in my own module's code index.ts:

export type BlufiClient = com.esp32.blufi.BlufiClient;

The auto-generated declarations file index.d.ts:

export declare type BlufiClient = com.esp32.blufi.BlufiClient;

The goal here is to allow the users of my module to do something like:

import { BlufiClient } from 'nativescript-blufi';

const client = new BlufiClient(...);

But the error I get when trying to new the class is:

'BlufiClient' only refers to a type, but is being used as a value here.

Initially, I thought I had to export type BlufiClient = typeof ... but that didn't work, and it makes sense that it didn't because it's pointing to a class, not a constructor or a member.

How can I achieve this?

To attempt to illustrate the issue more clearly, here is an example project you can run in your browser: https://playcode.io/849389

In this example, native_module.ts is the module I have no control over, and must remain as-is.

my_module.ts is where I am trying to "re-export" the classes in native_module.ts so that they are available without having to write out the full module path (i.e. com.esp32.blufi...). I also hope to add some additional functionality in my_module that isn't available in native_module in the future.

Finally, test_module.ts simply tries to instantiate the "re-exported" class. You can see the error I am getting.

0

There are 0 answers