Skip to content

[Feature Request] Support export assignment when other exported elements are just type declarations #31780

Closed
@AnyhowStep

Description

@AnyhowStep

Search Terms

export assignment, export element, type declaration

Suggestion

//type.ts
export type A = "A";
//type2.ts
export interface B {
    x : number,
}
//index.ts
export = {
    prop0 : "prop0",
    prop1 : "prop1",
};
export * from "./type";
import {B} from "./type2";
export {B};

Right now, export * from "./type"; and export {B}; will give the following error,

An export assignment cannot be used in a module with other exported elements.ts(2309)

To me, it makes sense to allow export assignment with other exported elements when the other exported elements are strictly interfaces/type aliases.

This way, you can use the typedefs and variables of the package during compile-time.
And those typedefs can be erased in the emitted .js files.

Use Cases

Sometimes, I simply have to use "export assignment". The object I want to export as a module has hundreds of variables, generated via run-time (not compile-time). The type of the generated object is something like { [k in keyof SomeOtherImport] : SomeWrapper<SomeOtherImport[k]> }

I want to export this object, but also export some typedefs.

However, right now, this isn't supported.

Users that want to use the wrapper would simply have to import two things,

import * as wrapped from "some-package/wrapped";
import * as pkg from "some-package";

declare const x : pkg.SomeTypeDef;
wrapped.someFunction();

However, I want users to be able to do,

import * as pkg from "some-package/wrapped";

declare const x : pkg.SomeTypeDef;
pkg.someFunction();

Examples

Example 1:

//function.ts
export function foo () {}

This should work, because foo is ultimately not used during run-time.

//index.ts
import {foo} from "./function.ts";
type Foo = typeof foo;
export {Foo};
export = { x : 1, y : 2 };

TODO More examples

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    SuggestionAn idea for TypeScriptToo ComplexAn issue which adding support for may be too complex for the value it adds

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions